2017-02-03 99 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