GarouDan
GarouDan

Reputation: 3833

How to escape ejs tags inside an ejs template string?

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 complex things, than when rendered the result becomes:

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

Upvotes: 2

Views: 3887

Answers (1)

illcrx
illcrx

Reputation: 814

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

Here are the docs: EJS

If I understand your question correctly this should help.

Upvotes: 6

Related Questions