如何注释EJS代码(JS节点)而不出现错误

问题描述 投票:0回答:6

我的 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)

如何评论这段代码?

javascript node.js ejs
6个回答
45
投票

有两种解决方案:

  • <%# comment %>
    (来自文档
  • <%/* comment */%>
    (也可以,但是很丑而且用起来不舒服)

除了在 IDE 中突出显示语法之外,我没有看到这些示例之间的区别(使用 Brackets IDE 的示例)。


34
投票

多行

<% /* */ %>
格式示例。

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

10
投票

它还说here关于评论,您可以像下面这样评论:

 <%# code %>

9
投票

有两种方法可以做到!

正如EJS文档中提到的:

<%# commented out code %>

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

例如:

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

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

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

5
投票

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

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

0
投票

这里是注释掉ejs代码的一种方法:

鉴于这行代码:

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

我这样做了:

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

或者: 原代码:

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

注释掉代码:

<!--label for= <%#=`${user.id}` %> style= <%#=`background-color: ${user.color};`%> -->
© www.soinside.com 2019 - 2024. All rights reserved.