How to render html content with erb tags in rails?

£可爱£侵袭症+ 提交于 2019-12-24 16:25:07

问题


I'm writing simple cms engine, and having a problem with dynamic content rendering, for example I prepare some page content with erb tags and helpers, how I can evaluate them in my view? Thanks.


回答1:


if i understand you correctly, you want to store snippets that contain ERB markup somewhere and at runtime evaluate them in your real templates of your rails app.

in this case, i think you will have to invoke ERB by hand. this is not really hard:

require 'erb'
name = "Rasmus"
template_string = "My name is <%= name %>"
template = ERB.new template_string
puts template.result # prints "My name is Rasmus"

read more in this nice writeup http://rrn.dk/rubys-erb-templating-system



来源:https://stackoverflow.com/questions/19312487/how-to-render-html-content-with-erb-tags-in-rails

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