Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to comment ejs code ( JS node)

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?

like image 480
Toumi Avatar asked Mar 21 '15 08:03

Toumi


People also ask

How do I comment multiple lines in node JS?

Multiline (Block) Comments Javascript multiline comments, also known as block comments, start with a forward slash followed by an asterisk (/*) and end with an asterisk followed by a forward slash (*/).

What is a comment in node JS?

JavaScript comments can be used to explain JavaScript code, and to make it more readable. JavaScript comments can also be used to prevent execution, when testing alternative code.


5 Answers

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

like image 148
Kas Elvirov Avatar answered Oct 20 '22 05:10

Kas Elvirov


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>
<% */ %>
like image 26
Quang Van Avatar answered Oct 20 '22 05:10

Quang Van


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

 <%# code %>
like image 10
A.B Avatar answered Oct 20 '22 07:10

A.B


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') %>
like image 8
Abhishek Gautam Avatar answered Oct 20 '22 05:10

Abhishek Gautam


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') %> 
    <%} %>
like image 5
Liam Kernighan Avatar answered Oct 20 '22 07:10

Liam Kernighan