如何在Rails中使用erb标签呈现HTML内容? [英] How to render html content with erb tags in rails?
问题描述
我正在编写简单的cms引擎,并在动态内容呈现方面遇到问题,例如,我准备了一些带有erb标签和帮助器的页面内容,如何在我的视图中对其进行评估?
谢谢。
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.
推荐答案
如果我对您的理解正确,则希望在运行时将包含ERB标记的代码段存储在某个位置在您的Rails应用程序的真实模板中评估它们。
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.
在这种情况下,我认为您将必须手动调用ERB。这并不难:
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"
在此精美文章中进一步了解 http://rrn.dk/rubys-erb-模板系统
read more in this nice writeup http://rrn.dk/rubys-erb-templating-system
这篇关于如何在Rails中使用erb标签呈现HTML内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!