Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print raw html strings on EJS

I'm using express.js with EJS templates and i'm trying to do something like this:

<%= "<a href='#'>Test</a>" %> 

but it prints this:

&lt;a href='#'&gt;Test&lt;/a&gt; 

how can i print "html safe" strings?

like image 668
Rogerio Chaves Avatar asked Nov 14 '11 16:11

Rogerio Chaves


People also ask

Can I use HTML in EJS?

EJS is a simple templating language that lets you generate HTML markup with plain JavaScript. No religiousness about how to organize things.

What does <% mean in EJS?

<%= Outputs the value into the template (HTML escaped) <%- Outputs the unescaped value into the template.


2 Answers

You should use html code everywhere, and use the EJS tags only where you need dynamic data. Example:

<a href='<%= user.id %>'><%= user.name %</a> 

To specifically answer your question you can use <%- "<tags_here>" %> to output unescaped HTML data.

like image 182
alessioalex Avatar answered Sep 30 '22 05:09

alessioalex


for raw output html in ejs you can use this code

<%- "<a href='#'>Test</a>" %> 
like image 34
Ivan Zhirkov Avatar answered Sep 30 '22 06:09

Ivan Zhirkov