How can I convert erb to html?

强颜欢笑 提交于 2019-12-13 14:48:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!