【发布时间】:2017-02-08 09:24:58
【问题描述】:
我们如何在模板中转义 ejs 标签本身?
在sails 中我想将以下代码放在layout.ejs 中
<!--STYLES-->
<% if (typeof head_css != 'undefined') { %>
<%= head_css %>
<% } else { %>
<% if (typeof head_css_before != 'undefined') { %>
<%= head_css_before %>
<% } %>
<link rel="stylesheet" href="/styles/importer.css">
<% if (typeof head_css_after != 'undefined') { %>
<%= head_css_after %>
<% } %>
<% } %>
<!--STYLES END-->
但默认情况下,<!--STYLES--> 和 <!--STYLES END--> 中的所有内容都被此模板替换:
<link rel="stylesheet" href="%s">
如sails-linker.js。
所以我想代替模板<link rel="stylesheet" href="%s"> 放一些更复杂的东西,而不是渲染时的结果:
<!--STYLES-->
<% if (typeof head_css != 'undefined') { %>
<%= head_css %>
<% } else { %>
<% if (typeof head_css_before != 'undefined') { %>
<%= head_css_before %>
<% } %>
<link rel="stylesheet" href="/styles/importer.css">
<% if (typeof head_css_after != 'undefined') { %>
<%= head_css_after %>
<% } %>
<% } %>
<!--STYLES END-->
但问题是当我尝试类似
<%= special_code_here %><link rel="stylesheet" href="%s">
这是自动渲染的。我需要在 ejs 模板中转义 <%。
我们怎样才能做到这一点?
【问题讨论】:
-
您是否尝试过
&lt;和&gt;的HTML 实体,即&lt;和&gt;? -
@ScottMarcus 是的,我试过了。这个问题在 layout.ejs 文件中呈现为 html 实体,我应该有非转义的
<%。所以在浏览器中出现了我想要的代码,但作为纯文本。
标签: javascript ejs