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<
and>
? – 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
1 Answer
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.