eruby cutting off top / binding issue

T

Trans

I'm trying to use ERB bound to a controlled context, and it's failing
when it build a template within another template.
If you have any insights about this please let me know. I'm simply at
a loss.

This is a minimal model of the problem I'm having:

require 'erb'

class Context

def initialize(page)
@page = page
end

def render(*a)
@page.render(*a)
end

end

class Page

def initialize
@context = Context.new(self)
@binding = @context.instance_eval{ binding }
end

def in1
%{
WAY UP HERE
<%= render('in2') %>
WAY DOWN HERE
}
end

def in2
%{
RIGHT UP HERE
<%= render('in3') %>
RIGHT DOWN HERE
}
end

def in3
"IN THE MIDDLE"
end

def render(var)
input = eval(var)
template = ERB.new(input)
template.result(@binding)
end

end

puts Page.new.render('in1')

I'm getting:

IN THE MIDDLE
RIGHT DOWN HERE

WAY DOWN HERE

Instead of the expected:

WAY UP HERE

RIGHT UP HERE
IN THE MIDDLE
RIGHT DOWN HERE

WAY DOWN HERE

Why is it cutting off the top? It makes now sense. If I remove the
context by changing to:

template.result(binding)

Then it works fine.

T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top