【问题标题】:How can I convert erb to html?如何将erb转换为html?
【发布时间】:2012-07-20 03:21:43
【问题描述】:

想象一下,我在 rails 中有 @template,它是 ActionTemplate::View 的一个实例。

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

【问题讨论】:

  • 可能是render @template,但从未见过任何实际意义上使用的模板实例

标签: ruby-on-rails erb templating


【解决方案1】:

试试这个...

ERB.new(@template.source).result

ERB#new

【讨论】:

    【解决方案2】:

    嗯...不建议在 Rails 之外乱搞ActionView::Template.new。你需要预先设置大量的东西(initrender

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多