最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to escape ejs tags inside an ejs template string? - Stack Overflow

programmeradmin0浏览0评论

How can we escape the ejs tags itself inside a template?

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

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

as in the sails-linker.js.

So instead of the template <link rel="stylesheet" href="%s"> I would like put something some more plex things, than when rendered the result bees:

<!--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.

How can we do this?

How can we escape the ejs tags itself inside a template?

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

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

as in the sails-linker.js.

So instead of the template <link rel="stylesheet" href="%s"> I would like put something some more plex things, than when rendered the result bees:

<!--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.

How can we do this?

Share Improve this question asked Feb 3, 2017 at 22:28 GarouDanGarouDan 3,83310 gold badges50 silver badges78 bronze badges 2
  • Have you tried the HTML entities for < and >, which are &lt; and &gt;? – Scott Marcus Commented Feb 3, 2017 at 22:38
  • @ScottMarcus Yes, I tried. The problem with this is was rendered as html entities in the layout.ejs file and there I should have the non-escaped <%. So in the browser appear the code I would like to have but as a plain text. – GarouDan Commented Feb 3, 2017 at 22:51
Add a ment  | 

1 Answer 1

Reset to default 6

<%% escapes the EJS tags, and will give you <%

Here are the docs: EJS

If I understand your question correctly this should help.

发布评论

评论列表(0)

  1. 暂无评论