mde / ejs

Embedded JavaScript templates -- http://ejs.co
Apache License 2.0
7.71k stars 846 forks source link

How to escape ejs tag within a string? #615

Closed anhlu8 closed 3 years ago

anhlu8 commented 3 years ago
<%
   let myString = "this is a string %> with special characters";
   let myOtherString = "this is a string <% with special characters";
   // Do something
%>

I need to use these special characters in my JS string <% and/or %> . But EJS template would return errors: Could not find matching close tag for "<%" or SyntaxError: Invalid or unexpected token while compiling ejs for %>

How should I go about this?

RyanZim commented 3 years ago

See https://github.com/mde/ejs/blob/master/docs/syntax.md#literal-tags for details. In your example, you would write:

<%
   let myString = "this is a string %%> with special characters";
   let myOtherString = "this is a string <%% with special characters";
   // Do something
%>