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

时间:2015-03-21 08:12:23

标签: javascript node.js ejs

我在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行仍有错误。这是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)

如何评论此代码?

6 个答案:

答案 0 :(得分:17)

有两种解决方案:

  • <%# comment %>(来自documentation
  • <%/* comment */%>(它也有效,但使用起来非常难看且不舒服)

除了突出显示IDE中的语法(使用Brackets IDE的示例)之外,我不会看到这些示例之间的差异

enter image description here

答案 1 :(得分:12)

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

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

答案 2 :(得分:6)

关于评论也说here

你可以评论如下:

 <%# code %>

答案 3 :(得分:2)

我觉得这对我有帮助。它简单,多线,不与任何东西发生冲突。

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

答案 4 :(得分:2)

两种方法!

如EJS文档中所述。

<%#注释出代码%>

<%/ *多行注释掉了代码* /%>

例如:

<%# include('includes/head.ejs') %>
</head>

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

<%- include('includes/end.ejs') %>

答案 5 :(得分:0)

这很难看,但确实有效:

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