简体   繁体   English

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

[英]How to comment ejs code ( JS node)

I have this code in an ejs File. 我在ejs文件中有这个代码。

<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 第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)

How can I comment this code ? 我该如何评论这段代码?

There is two solutions: 有两种解决方案:

  • <%# comment %> ( it's from documentation ) <%# comment %> (来自文档
  • <%/* comment */%> ( it works too but it's pretty ugly and uncomfortable for use ) <%/* comment */%> (它也有效,但使用起来非常难看和不舒服)

I don't see difference between those examples except highlighting syntax in IDE ( example with Brackets IDE ) 除了突出显示IDE中的语法(带有Brackets IDE的示例)之外,我看不出这些示例之间的区别

在此输入图像描述

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

it says here about the comments as well 在这里也说了这些评论

you can comment like below : 你可以评论如下:

 <%# code %>

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') %> 
    <%} %>

Two ways you can do! 你有两种方法可以做!

As mentioned in the Documentation of EJS. 如EJS文档中所述。

<%# commented out code %> <%#注释掉了代码%>

<%/* multiple lines commented out code*/%> <%/ *多行注释掉代码* /%>

eg: 例如:

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

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

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

This is ugly but it works: 这很丑,但它有效:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM