I would like to write code roughly like this in a template <% collapsible_fieldset( :legend => ''Address'', :except => ''new'', :valid => @address) do %> <%= render :partial => ''shared/address'', :object => @address %> <% end %> The nested code should work just as usual, but I want to wrap it in <fieldset ...> <legend>Address</legend> <!-- content --> </fieldset> The actual attribute values of the fieldset tag are intended to depend on the current action and on wether the address object is valid. Assuming these conditions are met, the start tag would look like this <fieldset class="collapsible initiallycollapsed"> Which is picked up by some nicely unobtrusive script in the browser to collapse the fieldset to just its legend. I''ve got all the individual pieces, but I don''t see how to wrap them up with some syntactic sugar as shown above. Michael -- Michael Schuerig Those people who smile a lot mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org Watch the eyes http://www.schuerig.de/michael/ --Ani DiFranco, Outta Me, Onto You
On Jul 29, 2005, at 11:05 AM, Michael Schuerig wrote:> > I would like to write code roughly like this in a template > > <% collapsible_fieldset( > :legend => ''Address'', > :except => ''new'', > :valid => @address) do %> > <%= render :partial => ''shared/address'', :object => @address %> > <% end %> > > The nested code should work just as usual, but I want to wrap it in > > <fieldset ...> > <legend>Address</legend> > <!-- content --> > </fieldset> ><snip>> I''ve got all the individual pieces, but I don''t see how to wrap > them up > with some syntactic sugar as shown above. > > Michael >I think this might be what you''re looking for: # in a helper.rb: def collapsible_fieldset(options = {}, &block) insert = Proc.new {|text| concat text, block.binding} insert["<fieldset>"] captured_text = capture(&block) insert[captured_text] insert["</fieldset>"] end In addition, you can actually pass parameters *back* to your block if you call the capture method with parameters: captured_text = capture(info, &block) This latter feature relies on a patch that was recently applied to the Rails code base (see http://dev.rubyonrails.com/changeset/1459). I think it''s in Rails 0.13.1 already. Duane Johnson (canadaduane)
On Friday 29 July 2005 19:21, Duane Johnson wrote:> I think this might be what you''re looking for: > > # in a helper.rb: > > def collapsible_fieldset(options = {}, &block) > insert = Proc.new {|text| concat text, block.binding} > insert["<fieldset>"] > captured_text = capture(&block) > insert[captured_text] > insert["</fieldset>"] > endThanks, that works ... and gave me a serious headache until I noticed that Proc#[] is a synonym for Proc#call. My resulting code looks slightly different, nevertheless def collapsible_fieldset(options = {}, &block) html = ''<fieldset>'' html << capture(&block) if block_given? html << ''</fieldset>'' Binding.of_caller do |binding| concat(html, binding) end end Michael -- Michael Schuerig Those people who smile a lot mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org Watch the eyes http://www.schuerig.de/michael/ --Ani DiFranco, Outta Me, Onto You
> def collapsible_fieldset(options = {}, &block) > html = ''<fieldset>'' > html << capture(&block) if block_given? > html << ''</fieldset>'' > Binding.of_caller do |binding| > concat(html, binding) > end > endA bit off topic but fyi: I recommend using the tag and content_tag helpers in helpers instead of plain html emitting:> def collapsible_fieldset(options = {}, &block) > html = content_tag :fieldset, (capture(&block) if block_given?) > Binding.of_caller do |binding| > concat(html, binding) > end > end-- Tobi http://www.snowdevil.ca - Snowboards that don''t suck http://typo.leetsoft.com - Open source weblog engine http://blog.leetsoft.com - Technical weblog
On Saturday 30 July 2005 16:09, Tobias Luetke wrote:> I recommend using the tag and content_tag > helpers in helpers instead of plain html emitting:Any particular reasons? Michael -- Michael Schuerig This is not a false alarm mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org This is not a test http://www.schuerig.de/michael/ --Rush, Red Tide