如何在 Slim 模板中渲染 HTML [英] How to render HTML inside Slim templates
问题描述
我正在尝试呈现以图标开头的链接.我正在使用 Slim 模板引擎和 Bootstrap CSS.
I'm trying to render a link preceded by an icon. I'm using Slim templating engine along with Bootstrap CSS.
通常您可以通过以下方式执行此操作:
Usually you could do this the following way:
<a href="#"><i class="icon-user"></i> My Profile</a>
根据 Slim 的文档,我们可以使用 ==
进行渲染而无需转义 HTML.因此,将其转换为 Slim,我尝试了以下变体:
According to Slim's documentation, we can use ==
to render without escaping HTML. So, translating this to Slim, I tried the following variations:
li== link_to "<i class='icon-user'></i> My Profile", current_user
li== link_to "#{'<i class="icon-user"></i>'.html_safe} My Profile", current_user
li= link_to "#{'<i class="icon-user"></i>'.html_safe} My Profile", current_user
呈现的所有变体<a href="/users/1"><i class="icon-user"></i>我的个人资料
转义 i
标签.
All variations rendered <a href="/users/1"><i class="icon-user"></i> My Profile</a>
escaping the i
tag.
如何阻止 Slim 或 Rails 转义 html?
How can I stop Slim or Rails from escaping html?
(Rails 3.2 与 Slim 1.2.1)
(Rails 3.2 with Slim 1.2.1)
推荐答案
您想禁用 link_to
参数的 HTML 转义,而不是整个 link_to
结果.您与 html_safe
非常接近,但是您的字符串插值正在吞噬您的HTML 安全"标志.这应该会更好:
You want to disable HTML escaping for the link_to
argument, not the entire link_to
result. You're pretty close with your html_safe
but your string interpolation is eating your "safe for HTML" flag. This should work better:
li= link_to '<i class="icon-user"></i> My Profile'.html_safe, current_user
这篇关于如何在 Slim 模板中渲染 HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!