如何将erb转换为html?
想象一下在rails中我有@template
,它是ActionTemplate::View
一个实例。
问题是:如何将@ template.source为
@template
为
hello from erb
?? 谢谢
试试这个…
ERB.new(@template.source).result
ERB#新
嗯…在Rails之外ActionView::Template.new
用ActionView::Template.new
并不是真的值得推荐。 你需要事先设置大量的东西( init和渲染 )
如果您确实想要使用ERB ,那么请使用此示例
require 'erb' x = 42 template = ERB.new <<-EOF The value of x is: <%= x %> EOF puts template.result(binding)
并且,您可以使用Kyle的答案从您的模板转到ERB。