如何使用原始 html 显示内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4052906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to display content with raw html
提问by pierrotlefou
@post.body
has following content (which is converted from Markdown by using RDiscount).How should I render it to the user in what it means? i.e I want to render it as strong textemphasized text...
@post.body
具有以下内容(使用 RDiscount 从 Markdown 转换而来)。我应该如何将其呈现给用户的含义?即我想将它呈现为强文本强调文本......
<p><strong>strong text</strong> </p> <p><em>emphasized text</em> </p> <blockquote> <p>this is a quote</p> </blockquote><p><img src="http://www.picturehouse.com/titles/images/rock.jpg" alt="alt text" title="" /> </p>
Using <%= @post.body =>
will only display it as the text shown above.
使用<%= @post.body =>
只会将其显示为如上所示的文本。
回答by mikej
Assuming Rails 3, use the rawhelper method e.g.
假设 Rails 3,使用原始帮助方法,例如
<%= raw(@post.body) %>
<%= raw(@post.body) %>
Escaping HTML output is on by default in all view templates (in contrast to earlier versions where you had to use the h
method to escape strings individually.)
默认情况下,所有视图模板中都启用转义 HTML 输出(与早期版本相反,您必须使用该h
方法单独转义字符串。)
回答by alex.zherdev
Are you using rails 3? It automatically escapes all contents of <%= %>
tags. To avoid it, do
你在使用 Rails 3 吗?它会自动转义<%= %>
标签的所有内容。为了避免它,请执行
<%= raw(@post.body) %>
回答by Jaime Bellmyer
I take it you're in Rails 3? One big change is that displayed text used to be raw by default, and you had to sanitize it yourself. Now it's the other way around. Call it like this:
我认为你在 Rails 3 中?一个很大的变化是显示的文本默认是原始的,你必须自己清理它。现在情况正好相反。像这样调用它:
<%= raw(@post.body) %>
And you'll get what you're looking for.
你会得到你想要的。
回答by CTS_AE
Quick, Easy, & to the Point
快速、简单、切中要害
<%== @post.body %>
More Information
<%== @post.body ==>
is an alias to<%= raw(@post.body) ==>
https://edgeguides.rubyonrails.org/active_support_core_extensions.html#output-safety
更多信息
<%== @post.body ==>
是别名<%= raw(@post.body) ==>
https://edgeguides.rubyonrails.org/active_support_core_extensions.html#output-safety