Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
481 views
in Technique[技术] by (71.8m points)

node.js - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...