问题
Imagine in rails I have @template
that is an instance of ActionTemplate::View
.
Question is: How can I convert @template
whose @template.source is <%= "hello from erb" %>
to hello from erb
?? thanks
回答1:
Try this...
ERB.new(@template.source).result
ERB#new
回答2:
Well... messing around with ActionView::Template.new
outside of Rails is not really recommended. You need a ton of stuff setup beforehand (init and render)
If you do want to just use ERB, then go with this example
require 'erb'
x = 42
template = ERB.new <<-EOF
The value of x is: <%= x %>
EOF
puts template.result(binding)
And, you can use Kyle's answer to go from your template to ERB.
来源:https://stackoverflow.com/questions/11572396/how-can-i-convert-erb-to-html