Hi everyone,
I am writing a helper function that takes a parameter and block and
wraps some HTML around it. e.g.
def box_with_caption(caption, &block)
data = capture(&block)
buffer = render_partial("helpers/box_caption", :caption =>
caption)
buffer << render_partial("helpers/box_data", :data => data)
buffer
end
however, when I attempt the following, it doesn''t work.
<%= box_with_caption("Caption here") do %>
HTML for the box in here.
<% end %>
To make this work I need to do something like:
<% box = box_with_caption("Caption here") do %>
HTML for the box in here.
<% end %>
<%= box %>
Anyone know how to manage this without having to do the variable assign
and then output?
Thanks,
Wayne
On Jun 28, 2005, at 8:02 PM, Wayne Robinson wrote:> Hi everyone, > > I am writing a helper function that takes a parameter and block and > wraps some HTML around it. e.g. > > def box_with_caption(caption, &block) > data = capture(&block) > buffer = render_partial("helpers/box_caption", :caption => > caption) > buffer << render_partial("helpers/box_data", :data => data) > buffer > end >Try something like this instead: def box_with_caption(caption, &block) insert = Proc.new {|text| concat text, block.binding} insert[render_partial("helpers/box_caption", :caption => caption)] insert[render_partial("helpers/box_data", :data => capture(&block)] end Then call it like this (note that there is no = sign after the <%): <% box_with_caption("Caption here") do %> HTML for the box <% end %> Duane Johnson (canadaduane) _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Thanks, I was looking for this but I couldn''t find what to use for
binding in concat.
- Wayne
________________________________
From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On
Behalf Of Duane Johnson
Sent: Thursday, 30 June 2005 12:40 AM
To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: Re: [Rails] Complex helper with block help
On Jun 28, 2005, at 8:02 PM, Wayne Robinson wrote:
Hi everyone,
I am writing a helper function that takes a parameter and block
and
wraps some HTML around it. e.g.
def box_with_caption(caption, &block)
data = capture(&block)
buffer = render_partial("helpers/box_caption", :caption =>
caption)
buffer << render_partial("helpers/box_data", :data =>
data)
buffer
end
Try something like this instead:
def box_with_caption(caption, &block)
insert = Proc.new {|text| concat text, block.binding}
insert[render_partial("helpers/box_caption", :caption =>
caption)]
insert[render_partial("helpers/box_data", :data =>
capture(&block)]
end
Then call it like this (note that there is no = sign after the <%):
<% box_with_caption("Caption here") do %>
HTML for the box
<% end %>
Duane Johnson
(canadaduane)
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails