【问题标题】:How to use comments in Handlebar templates?如何在 Handlebar 模板中使用注释?
【发布时间】:2013-02-26 11:38:52
【问题描述】:

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

【问题讨论】:

  • This pull request 你可能会感兴趣。
  • 它确实支持多行 cmets 但不会忽略其中的表达式。

标签: javascript html templates handlebars.js


【解决方案1】:

最新版本的 Handlebars 支持 block cmets:

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

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

【讨论】:

  • 感谢 jptsetung。它并没有像我期望的那样完全起作用,但我想它可以达到预期的目的。通过本教程了解车把如何在幕后工作。 net.tutsplus.com/tutorials/javascript-ajax/… 。似乎车把并没有完全忽略注释行,因为它仍在创建令牌。令牌只是空的。例如 "{{!-- Hello {{name}} --}}" 创建 1 个空令牌。只是认为如果它曾经给任何人带来问题是值得一提的。不知道会不会,因为它在幕后。
  • @jptsetung,你能帮忙解决一下这个stackoverflow.com/questions/70820997/…
【解决方案2】:

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

正常表达式:

{{expressions}}

评论表达:

{{!expressions}}

【讨论】:

  • 请注意{{!{{!-- 形式的区别在于只有后者允许嵌入把手标签。因为这很容易忘记,尤其是在稍后修改的长评论中,我建议始终使用较长的评论形式。
【解决方案3】:

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

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

cmets 不会出现在结果输出中。如果您想显示 cmets,请使用 HTML cmets。

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

refer this link to

【讨论】:

    【解决方案4】:

    handlebar.js 的两种注释方式

    单个组件:

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

    多组件:

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

    【讨论】:

      【解决方案5】:

      使用此代码:

      {{#data}}
      <!-- enter comments here  -->
      <p>{{name}}</p>
      {{/data}}  
      

      【讨论】:

        猜你喜欢
        • 2013-03-03
        • 1970-01-01
        • 2012-12-19
        • 2010-10-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多