如何在Handlebar模板中使用注释?

时间:2013-03-11 10:22:09

标签: javascript html templates handlebars.js

我使用Handlebar.js作为我的模板引擎。现在我想在我的车把模板中注释掉一些块。但后来我意识到Handlebar不会忽略Handlebar注释块中的表达式。对此有何解决方法?

5 个答案:

答案 0 :(得分:176)

最新版本的Handlebars具有块评论支持:

{{!-- {{commented expressions}} --}}

https://github.com/wycats/handlebars.js/commit/a927a9b0adc39660f0794b9b210c9db2f7ddecd9

答案 1 :(得分:48)

只需在左括号后添加感叹号即可。

正常表达:

{{expressions}}

评论表达:

{{!expressions}}

答案 2 :(得分:22)

在车把模板文件中使用这种方式。

<div class="entry">
  {{!-- only output author name if an author exists --}}
  {{#if author}}
    <h1>{{author.firstName}} {{author.lastName}}</h1>
  {{/if}}
</div>

评论不会出现在结果输出中。如果您希望显示评论,请使用HTML评论。

<div class="entry">
  {{! This comment will not be in the output }}
  <!-- This comment will be in the output -->
</div>

refer this link to

答案 3 :(得分:0)

handlebar.js 的两种评论方式

单个组件:

{{!fixedTop=true}}     --> comments the whole content inside the brackets

多个组件:

    {{!--fixedTop=true
          alignment=true--}}     --> comments the whole content until end with "--"

答案 4 :(得分:-3)

使用此代码:

{{#data}}
<!-- enter comments here  -->
<p>{{name}}</p>
{{/data}}  
相关问题