34

I have this code in an EJS file:

<table> <% for(var i=0; i < data.length; i++) { %> <tr> <td><%= data[i].id %></td> <td><%= data[i].name %></td> </tr> <% } %> </table> 

When I comment it this way,

<!-- <table> --> <!-- <% for(var i=0; i < data.length; i++) { %> --> <!-- <tr> --> <!-- <td><%= data[i].id %></td> --> <!-- <td><%= data[i].name %></td> --> <!-- </tr> --> <!-- <% } %> --> <!-- </table> --> 

I still have an error in Line 2. Here is the stack of the error:

ReferenceError: c:\Users\toumi\Desktop\workspaces\eclipse\ToDoList\views\x.ejs:2 1| <!-- <table> --> >> 2| <!-- <% for(var i=0; i < data.length; i++) { %> --> 3| <!-- <tr> --> 4| <!-- <td><%= data[i].id %></td> --> 5| <!-- <td><%= data[i].name %></td> --> data is not defined at eval (eval at <anonymous> (c:\Users\toumi\Desktop\workspaces\eclipse\ToDoList\node_modules\ejs\lib\ejs.js:455:12), <anonymous>:11:25) at c:\Users\toumi\Desktop\workspaces\eclipse\ToDoList\node_modules\ejs\lib\ejs.js:482:14 at View.exports.renderFile [as engine] (c:\Users\toumi\Desktop\workspaces\eclipse\ToDoList\node_modules\ejs\lib\ejs.js:348:31) at View.render (c:\Users\toumi\Desktop\workspaces\eclipse\ToDoList\node_modules\express\lib\view.js:93:8) at EventEmitter.app.render (c:\Users\toumi\Desktop\workspaces\eclipse\ToDoList\node_modules\express\lib\application.js:566:10) at ServerResponse.res.render (c:\Users\toumi\Desktop\workspaces\eclipse\ToDoList\node_modules\express\lib\response.js:938:7) at c:\Users\toumi\Desktop\workspaces\eclipse\ToDoList\todoList.js:13:6 at Layer.handle [as handle_request] (c:\Users\toumi\Desktop\workspaces\eclipse\ToDoList\node_modules\express\lib\router\layer.js:82:5) at next (c:\Users\toumi\Desktop\workspaces\eclipse\ToDoList\node_modules\express\lib\router\route.js:110:13) at Route.dispatch (c:\Users\toumi\Desktop\workspaces\eclipse\ToDoList\node_modules\express\lib\router\route.js:91:3) 

How can I comment this code?

1
  • Please add what error you are receiving. I tried adding <!-- --> in ejs and i am not receiving any error. Commented Mar 21, 2015 at 8:51

6 Answers 6

49

There is two solutions:

  • <%# comment %> (it's from documentation)
  • <%/* comment */%> (it works too, but it's pretty ugly and uncomfortable for use)

I don't see a difference between those examples except highlighting syntax in an IDE (example with the Brackets IDE).

Enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

<%# <span class="form-text text-danger"><%- errors["email"].msg %></span> %> Could not find matching close tag for "<%#".
@IgorArnaut Could you clarify what are you trying achieve?
Comment the line so that even EJS TAG <%- errors["email"].msg %> does not executed.
Try this one <% /* %><%# <span class="form-text text-danger"><%- errors["email"].msg %></span> %><% */ %>
37

Sample of the <% /* */ %> format for multi-line.

<% /* %> <div> <span>This will not be rendered</span> <% for(var i=0; i < data.length; i++) { %> <span>These won't be rendered either.</span> <% } %> </div> <% */ %> 

4 Comments

This is the good answer to the question. The questioner clearly needed multiline comment with ejs tags inside that, and neither <%# ... %> nor <% /* ... */ %> (in this exact form) doesn't work in that case.
it's not working when there's variable <%= var %> inside it
When using ejs-loader with webpack, it works for single line and multi-line comments.
<% /* <span class="form-text text-danger"><%- errors["email"].msg %></span> */ %> Could not find matching close tag for "<%".
11

It says here about the comments as well that you can comment like below:

 <%# code %> 

Comments

11

There are two ways to do it!

As mentioned in the documentation of EJS:

<%# commented out code %> <%/* multiple lines commented out code*/%> 

For example:

<%# include('includes/head.ejs') %> </head> <body> <%# include('includes/navigation.ejs') %> <h1>Page Not Found!</h1> <%- include('includes/end.ejs') %> 

1 Comment

None of these work if the line has EJS output tags.
6

I found this helpful for me. It's simple, multiline and does not conflict with anything.

 <%if(false) {%> <ul> <% for(var i =1; i <= 10; i++) { %> <li> Hello this is iteraiton <%=i %> </li> <% }%> </ul> <%- include('./meow') %> <%} %> 

Comments

0

Here is one way to comment out ejs code:

Given this line of code:

<label for="<%= user.id %>" style="background-color: <%= user.color %>;"> 

I did this:

<!--label for="<%#= user.id %>" style="background-color: <%#= user.color %>;"--> 

Alternatively: Original code:

<label for= <%=`${user.id}` %> style= <%=`background-color: ${user.color};`%>> 

Commented out code:

<!--label for= <%#=`${user.id}` %> style= <%#=`background-color: ${user.color};`%> --> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.