> I''m trying to modularize some display functionality within my
Rails app. I
> need to be able to store an erb block, and then evaluate it 1 or
> more times
> at a later point in time. Two questions here, which are driving me crazy!
>
> 1) How can I store an erb block captured using do for later use? Regular
> variable assignment doesn''t appear to work.
>
> def content(&block)
> @raw_content = block
> end
>
> The above does not work. However, if I evaluate ''block''
within the content
> method call, then the evaluation works. If I try to evaluate
> @raw_content at
> a later point through another function it just returns nothing. So how
> exactly do I store ''block'' for later use?
>
> 2) When evaluating ''block'', how can I get it to use the
variables present
> within the scope of the calling function, or at least within the calling
> class? As it stands now, it will only take variables that have
> been defined
> in the controller, and not my class.
>
> .rhtml file
> <%r = Renderer.new()
> r.content do%>
> <%=@sometext%>
> <%end%>
> <%=r.render()%>
>
> .rb file
> class SomeController < ApplicationController
> def blah()
> @sometext = "blah"
> end
> end
>
> class Renderer
> def content(&block)
> @sometext = "content"
> buffer = eval("_erbout", block.binding);
> pos = buffer.length
> block.call
>
> @data = buffer[pos..-1]
>
> buffer[pos..-1] = ""
> end
>
> def render()
> @data
> end
> end
>
> The above will render "blah", and not "content", even
if the definition of
> @sometext is removed from the controller, the eval will not take from the
> class.
>
> Any help would be greatly appreciated.
How can I get around these scoping issues? Is there a better place to ask
questions about erb? I thought this would be the proper place, since there
are tie-ins with Rails, but maybe it''s not?
Thanks,
Steve