2017-02-03 197 views
0

我们如何才能逃避模板内的ejs标签?如何逃避ejs模板字符串内的ejs标签?

在船帆我愿把下面的代码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模板中的<%

我们该怎么做?

+0

您是否尝试过'<' and '>'的HTML实体,它们是'<'和'>'? –

+0

@ScottMarcus是的,我试过了。这个问题被呈现为layout.ejs文件中的html实体,在那里我应该有非转义的'<%'。所以在浏览器中出现我想要的代码,但是作为纯文本。 – GarouDan

回答

1

<%%逃脱EJS标签,并会给你<%

下面是文档: EJS

如果我明白你的问题正确这应该帮助。

+0

我已经试过这个,并没有工作。看起来像帆(我使用的框架在'ejs-locals'中使用了旧版本的ejs)。我试图更新ejs当地人的ejs,但仍然无法正常工作。但是因为我的问题是关于ejs的,我会接受这个答案。我在ejs上讨论过这个问题,这个人解决了这个问题,并没有帮我解决这个问题。 – GarouDan