Erb recursion

Hi !

I found some solutions to create recursion in erb templates.

What do you think of these solutions ? What problems could there be ?
I have a small preference for the first solution since it keeps all
the context available inside the “lambda”.

Solution 1:
<% a = lambda do |var| %>
hello ‘<%= var %>’
<% end %>

<%‘baba’) %>
<% %>

Solution 2:
<% def a_fun(var); _erbout = ‘’; %>
hello again, ‘<%= var %>’
<% end %>

<%= a_fun(‘baba’) %>
<%= a_fun(man) %>

Thanks for your comments !


Here is a small test file with these two solutions. As you can see
from running the file, the “lambda” solution does not work. The
“method definition” works fine (but I loose all the local variables in
the definition.

Another solution would be to capture binding before method definition
and pass it inside the method (but this looks a lot like a block.

To the clever minds out there: is there a way to fix the first
“lambda” solution ?

<% a = lambda do |var| %><<%= %>>
<% var.children.each do |var2| %><%= %><% end %></<%= %>>
<% end %>

<% %>
( see pastie above for data format )


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs