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