Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering ejs template

Tags:

node.js

ejs

I have following code in nodejs (I read temp.ejs file and get content as ejsHtml as string):

var html = EJS.render(ejsHtml, { A: '<div>smth</div>' } );

And in temp.ejs:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title></title>
</head>
<body>
      <%= A %>
</body>
</html>

Output:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title></title>
</head>
<body>
      &lt;div&gt; smth &lt;/div&gt;
</body>
</html>

Please tell me how to get Html and not that

like image 951
karaxuna Avatar asked Apr 27 '12 18:04

karaxuna


2 Answers

For outputting escaped html, you do the following:

<%= code %>

To output unescaped html, you would use the following

<%- code %>
like image 92
Menztrual Avatar answered Sep 26 '22 08:09

Menztrual


I used underscore to help me out. Only <%= or <%- did not work.

<%- _.unescape( data.textWithHtml ) %>
like image 41
Gustav Avatar answered Sep 22 '22 08:09

Gustav