Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference between <%=, <% and <%- in ejs [duplicate]

As I wrote in the title. In Ejs what's the difference between <%=, <% and <%-? for example I saw this code <% include ../partials/header.ejs %>, and then there is this code <%= title %>. I also saw <%- somewhere but cannot find a code example anywhere. So what's the difference? When do I use which?

I found this but it's for ruby on rails Difference between <% %> and <%= %> in RoR

like image 824
Alex Ironside Avatar asked Jan 30 '18 13:01

Alex Ironside


People also ask

What does <% mean in EJS?

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

Can a JS script get a variable written in a EJS context page within the same file?

Yes, you cant: if it is on server.

What are EJS partials?

When using the default view engine ( ejs ), Sails supports the use of partials (i.e. "view partials"). Partials are basically just views that are designed to be used from within other views. They are particularly useful for reusing the same markup between different views, layouts, and even other partials.

Can we use EJS in script tag?

Data passed from the server is sent to the EJS file and then we can access that data using the below line and it will give that data to h, p, or another text tag. Now you can perform any operation on the data variable which has the same value as EJS passed data variable.


1 Answers

The following is from ejs docs (tag section):

  • <% 'Scriptlet' tag, for control-flow, no output
  • <%= Outputs the value into the template (HTML escaped)
  • <%- Outputs the unescaped value into the template

See the difference between escaped and unescaped html here

like image 140
YouneL Avatar answered Oct 06 '22 00:10

YouneL