# Erb recursion

**URL:** <https://www.ruby-forum.com/t/erb-recursion/131904>\
**Category:** Ruby\
**Created:** [March 14, 2008, 8:32am UTC](https://www.ruby-forum.com/t/erb-recursion/131904 "2008-03-14T08:32:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gazoduc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c5a1d2/32.png) [@gazoduc](https://www.ruby-forum.com/u/gazoduc)\
**Post date:** [March 14, 2008, 8:32am UTC](https://www.ruby-forum.com/t/erb-recursion/131904/1 "2008-03-14T08:32:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gazoduc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c5a1d2/32.png) [@gazoduc](https://www.ruby-forum.com/u/gazoduc)\
**Post date:** [March 14, 2008, 8:56am UTC](https://www.ruby-forum.com/t/erb-recursion/131904/2 "2008-03-14T08:56:20Z")

</div>

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](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
