I''m trying to pass an ERB block to a helper, and I''m not able
to achieve
the results I want. The helper should take the block and incorporate it
into some other text & html, before finally outputing it back to the ERB
template.
Here''s what I want:
--helper--
def helper (a, b)
yld = yield
out = --some function of a, b, and yld--
out
end
--ERB--
<% helper(''foo'', ''bar'') do %>
--html mixed with erb--
<% end %>
If I include the equal in <%=, I get errors. If I drop the equal, I lose
the output of the helper and just get the raw yield. I''ve tried every
possible combination of syntax here with no luck. Any thoughts?
wbr
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On 10/6/07, Bill Richardson <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I''m trying to pass an ERB block to a helper, and I''m not able to achieve > the results I want. The helper should take the block and incorporate it > into some other text & html, before finally outputing it back to the ERB > template. > > Here''s what I want: > > --helper-- > > def helper (a, b) > yld = yield > out = --some function of a, b, and yld-- > out > end > > --ERB-- > > <% helper(''foo'', ''bar'') do %> > --html mixed with erb-- > <% end %> > > If I include the equal in <%=, I get errors. If I drop the equal, I lose > the output of the helper and just get the raw yield. I''ve tried every > possible combination of syntax here with no luck. Any thoughts?I''m not an expert on this by any means, but something along these lines should work: def helper(a, b, &block) yld = capture(&block) out = ...build up string using a, b, and yld... concat out, block.binding end Use "<% helper", not "<%= helper" when you call it (no equal sign) HTH --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Bob Showalter wrote:> > I''m not an expert on this by any means, but something along these > lines should work: > > def helper(a, b, &block) > yld = capture(&block) > out = ...build up string using a, b, and yld... > concat out, block.binding > end > > Use "<% helper", not "<%= helper" when you call it (no equal sign) > > HTHAhh... I suspected it had something to do with ''capture'' and ''concat''. Your suggestion works, when I try my scaled down demo solution, which is a huge step forward. But my full-blown helper still bombs. Anyone know of a good resource on the finer workings of capture and concat? Or more generally on the interactions between controllers, helpers, and templates? I have books, and I''ve scoured the web, yet I''ve found very little documentation on this topic. Thanks -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ok, got it. I had a piece of bad code in the helper, and the error
produced looked like a problem with the block/capture/concat stuff.
Thanks for the help. Here''s what works:
--erb--
<% build_test(args, *options, &block) do %>
<b>some rhtml here</b>
<%# end %>
--helper--
def build_test (args, *options, &block)
x = args.to_s
y = capture(&block) if block_given?
y += x
y += "...some more text"
concat z, block.binding
end
WBR
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
... er um, sorry, the z should be a y. wbr -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---