问题
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