javascript 如何注释ejs代码(JS节点)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29180611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:10:54  来源:igfitidea点击:

How to comment ejs code ( JS node)

javascriptnode.jsejs

提问by Toumi

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 ?

我如何评论此代码?

回答by Artem Solovev

There is two solutions:

有两种解决方案:

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


I don't see difference between those examples except highlighting syntax in IDE ( example with Brackets IDE )

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

enter image description here

在此处输入图片说明

回答by Quang Van

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

回答by A.B

it says hereabout the comments as well

也在这里说明了评论

you can comment like below :

你可以像下面这样评论:

 <%# code %>

回答by Liam Kernighan

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

回答by Ronen

This is ugly but it works:

这很丑陋,但它有效:

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

回答by Abhishek Gautam

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