(Note: I am using Ruby's erb
, not that of Rails.)
When using the current binding to resolve an ERB template from within another ERB template, the text of the outer template that precedes the call to the inner one is lost.
For example:
inner.erb
file content:
inner
outer.erb
file content:
outer
<%= ERB.new(File.read('inner.erb')).result(binding) %>
outer
Test script erb-test
(must do chmod +x erb-test
):
#!/usr/bin/env ruby
require 'erb'
puts ERB.new(File.read('outer.erb')).result
The output of ./erb-test
is:
inner
outer
As shown, the first 'outer' string specified in the outer.erb
template file is lost.
If we remove the binding
from the ERB call in outer.erb
:
<%= ERB.new(File.read('inner.erb')).result %>
...then we now see the first outer
specified in the outer template:
outer
inner
outer
Is this a bug? How do you recommend addressing this? I realize it's better to limit the state available to templates, and usually I do that using ERB#result_with_hash
, but in the case of a sub-template, that is, a template called from another template, it seems reasonable to me that the inner template inherit the binding of the outer template, since it is already limited.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…