Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
想象一下,在我拥有的 Rails 中@template,这是ActionTemplate::View.
@template
ActionTemplate::View
问题是:如何将@template其@template.source转换<%= "hello from erb" %>为hello from erb?谢谢
<%= "hello from erb" %>
hello from erb
试试这个...
ERB.new(@template.source).result
ERB#新
嗯......ActionView::Template.new真的不推荐在 Rails 之外乱搞。你需要预先设置大量的东西(初始化和渲染)
ActionView::Template.new
如果您确实只想使用ERB,请使用此示例
require 'erb' x = 42 template = ERB.new <<-EOF The value of x is: <%= x %> EOF puts template.result(binding)
而且,您可以使用 Kyle 的答案从您的模板转到 ERB。