3

想象一下,在我拥有的 Rails 中@template,这是ActionTemplate::View.

问题是:如何将@template其@template.source转换<%= "hello from erb" %>hello from erb?谢谢

4

2 回答 2

5

试试这个...

ERB.new(@template.source).result

ERB#新

于 2012-07-20T03:34:47.320 回答
4

嗯......ActionView::Template.new真的不推荐在 Rails 之外乱搞。你需要预先设置大量的东西(初始化渲染

如果您确实只想使用ERB,请使用此示例

require 'erb'

x = 42
template = ERB.new <<-EOF
  The value of x is: <%= x %>
EOF
puts template.result(binding)

而且,您可以使用 Kyle 的答案从您的模板转到 ERB。

于 2012-07-20T03:42:26.337 回答