如何评论ejs代码(JS节点)

我在ejs文件中有这个代码。

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

当我这样评论的时候,

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

我仍然有一个错误的第2行。这是错误的堆栈

 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) 

我怎样才能评论这个代码?

有两个解决scheme:

  • <%# comment %> (来自文档 )
  • <%/* comment */%> (它也可以工作,但使用起来很丑,不舒服)

我没有看到这些例子之间的区别,除了突出IDE语法(Brackets IDE的例子)

在这里输入图像描述

它在这里也说了评论

你可以评论如下:

  <%# code %> 

我发现这对我有帮助。 这很简单,多行,不与任何冲突。

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

多行的<% /* */ %>格式的示例。

 <% /* %> <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> <% */ %> 

这是丑陋的,但它的作品:

 <%if (false) {%> <div> <span>This will not be rendered</span> </div> <%}%> 

像这样评论你不会收到任何错误

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