node.js 在 EJS 上打印原始 html 字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8124583/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:46:18  来源:igfitidea点击:

Print raw html strings on EJS

node.jsexpress

提问by Rogerio Chaves

I'm using express.js with EJS templates and i'm trying to do something like this:

我正在将 express.js 与 EJS 模板一起使用,并且正在尝试执行以下操作:

<%= "<a href='#'>Test</a>" %>

but it prints this:

但它打印了这个:

&lt;a href='#'&gt;Test&lt;/a&gt;

how can i print "html safe" strings?

如何打印“html 安全”字符串?

回答by alessioalex

You should use html code everywhere, and use the EJS tags only where you need dynamic data. Example:

您应该在任何地方使用 html 代码,并且只在需要动态数据的地方使用 EJS 标签。例子:

<a href='<%= user.id %>'><%= user.name %</a>

To specifically answer your question you can use <%- "<tags_here>" %>to output unescapedHTML data.

要专门回答您的问题,您可以使用<%- "<tags_here>" %>输出未转义的HTML 数据。

回答by Ivan Zhirkov

for raw output html in ejs you can use this code

对于 ejs 中的原始输出 html,您可以使用此代码

<%- "<a href='#'>Test</a>" %>