7

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

Alex Ironside
  • 4,658
  • 11
  • 59
  • 119

1 Answers1

20

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

YouneL
  • 8,152
  • 2
  • 28
  • 50
  • 1
    What is unescaped value? – Alex Ironside Jan 30 '18 at 14:29
  • Not sure what unescaped value means but `<%-` is for including other code, and this is the syntax for doing so: `<%- include('', ) %>`, e.g., `<%- include('../includes/end.ejs') %>` for the footer. – nCardot Mar 11 '20 at 04:34
  • 1
    Unescaped value could mean non-commented code, `<%-` filters out the commented portion while `<%=` includes commented code and prints value as is. – ShailyAggarwal Mar 11 '20 at 13:23