node.js 在 EJS 中将变量呈现为 HTML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10326950/
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
Render a variable as HTML in EJS
提问by MrJaeger
I am using the Forms library for Node.js (Forms), which will render a form for me on the backend as so:
我正在使用 Node.js 的 Forms 库(Forms),它将在后端为我呈现一个表单,如下所示:
var signup_form = forms.create({
username: fields.string({required: true})
, password: fields.password({required: true})
, confirm: fields.password({
required: true
, validators: [validators.matchField('password')]
})
, email: fields.email()
});
var signup_form_as_html = signup_form.toHTML();
The final line var signup_var signup_form_as_html = signup_form.toHTML();creates a block of HTML which looks as such:
最后一行var signup_var signup_form_as_html = signup_form.toHTML();创建了一个 HTML 块,如下所示:
<div class="field required"><label for="id_username">Username</label><input type="text" name="username" id="id_username" /></div><div class="field required"><label for="id_password">Password</label><input type="password" name="password" id="id_password" /></div><div class="field required"><label for="id_confirm">Confirm</label><input type="password" name="confirm" id="id_confirm" /></div><div class="field"><label for="id_email">Email</label><input type="text" name="email" id="id_email" /></div>
Basically just a long string of HTML. I then try to render it using EJS and Express using the following code:
基本上只是一长串HTML。然后我尝试使用以下代码使用 EJS 和 Express 渲染它:
res.render('signup.ejs', {
session: loginStatus(req)
, form: signup_form_as_html
});
But on rendering the HTML is simply the string that I posted above, rather than actual HTML (and thus a form as I want). Is there any way to make that string render as actual HTML using EJS? Or will I have to use something like Jade?
但是在呈现 HTML 时只是我在上面发布的字符串,而不是实际的 HTML(因此是我想要的表单)。有没有办法使用 EJS 使该字符串呈现为实际的 HTML?还是我必须使用像 Jade 这样的东西?
回答by Jakub Oboza
With ejs you can have
使用 ejs,您可以拥有
<% code %>
... which is code that is evaluated but not printed out.
...这是已评估但未打印出来的代码。
<%= code %>
... which is code that is evaluated and printed out (escaped).
...这是评估和打印出(转义)的代码。
<%- code %>
... which is code that is evaluated and printed out (not escaped).
...这是评估和打印出(未转义)的代码。
Since you want to print your variable and NOT escape it, your code would be the last type (with the -<%). In your case:
由于您想打印变量而不是对其进行转义,因此您的代码将是最后一种类型(带有-<%)。在你的情况下:
<%- my_form_content %>
For more, see the full ejs documentation
有关更多信息,请参阅完整的 ejs 文档
回答by Pavel Kovalev
October 2017 update
2017 年 10 月更新
The new ejs(v2, v2.5.7) development is happening here: https://github.com/mde/ejsThe old ejs (v0.5.x, 0.8.5, v1.0.0) is available here https://github.com/tj/ejs
该新EJS(V2,v2.5.7)的发展发生在这里:https://github.com/mde/ejs老EJS(v0.5.x,0.8.5,V1.0.0)可以在这里找到的https:/ /github.com/tj/ejs
Now with ejs you can do even more. You can use:
现在使用 ejs,您可以做更多事情。您可以使用:
- Escaped output with
<%= %>(escape function configurable) - Unescaped raw output with
<%- %> - Newline-trim mode ('newline slurping') with
-%>ending tag - Whitespace-trim mode (slurp all whitespace) for control flow with
<%_ _%> - Control flow with
<% %>
- 转义输出
<%= %>(转义功能可配置) - 未转义的原始输出
<%- %> - 带有
-%>结束标记的换行修剪模式(“换行啜饮”) - 用于控制流的空白修剪模式(吞掉所有空白)
<%_ _%> - 控制流
<% %>
So, in your case it is going to be <%- variable %>where variableis something like
所以,在你的情况下,它会是<%- variable %>哪里variable是一样的东西
var variable = "text here <br> and some more text here";
I hope this helps someone.
我希望这可以帮助别人。
回答by Ufenei augustine
I had the same issue with rendering the textarea input from from a wysiwyg editor saved as html in my database. The browser will not render it but displayed the html as text. After hours of searching, I found out
我在渲染来自所见即所得编辑器的 textarea 输入时遇到了同样的问题,该编辑器在我的数据库中保存为 html。浏览器不会呈现它,而是将 html 显示为文本。经过几个小时的搜索,我发现
<%= data %>escaped data while
<%= data %>转义数据而
<%- data %>left data 'raw'(unescaped) and the browser could now render it.
<%- data %>留下数据“原始”(未转义),浏览器现在可以呈现它。

