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 %>

<% a.call(‘baba’) %>
<% a.call(man) %>

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

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

Thanks for your comments !

Gaspard

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.

http://pastie.caboo.se/165584

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.name %>>
<% var.children.each do |var2| %><%= a.call(var2) %><% end %></<%=
var.name %>>
<% end %>

<% a.call(data) %>
( see pastie above for data format )

Gaspard

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