简体   繁体   English

如何在ejs模板字符串中转义ejs标签?

[英]How to escape ejs tags inside an ejs template string?

How can we escape the ejs tags itself inside a template? 我们如何在模板中逃避ejs标签?

In sails I would like to put the following code in the layout.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-->

but by default, all the things inside the <!--STYLES--> and <!--STYLES END--> are replaced by this template: 但默认情况下, <!--STYLES--><!--STYLES END-->都被此模板替换:

<link rel="stylesheet" href="%s">

as in the sails-linker.js . 就像在sails-linker.js

So instead of the template <link rel="stylesheet" href="%s"> I would like put something some more complex things, than when rendered the result becomes: 所以不是模板<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-->

But the problem is when I try something like 但问题是当我尝试类似的东西时

<%= special_code_here %><link rel="stylesheet" href="%s">

this is automatically rendered. 这是自动渲染的。 I need to escape the <% inside a ejs template. 我需要逃避ejs模板中的<%

How can we do this? 我们应该怎么做?

<%% escapes the EJS tags, and will give you <% <%%转义EJS标记,并会给你<%

Here are the docs: EJS 以下是文档: EJS

If I understand your question correctly this should help. 如果我理解你的问题,这应该会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM