我使用 Handlebar.js 作为我的模板引擎。现在我想注释掉我的车把模板中的一些块。但后来我意识到 Handlebar 并没有忽略 Handlebar 注释块中的表达式。有什么解决方法吗?
最新版本的 Handlebars 支持块评论:
{{!-- {{commented expressions}} --}}
https://github.com/wycats/handlebars.js/commit/a927a9b0adc39660f0794b9b210c9db2f7ddecd9
只需在左括号后添加一个感叹号即可。
正常表达:
{{expressions}}
评论表达:
{{!expressions}}
{{!
和 {{!--
表单之间的区别在于,只有后者允许嵌入把手标签。因为这很容易忘记,尤其是在稍后修改的长评论中,我建议始终使用较长的评论形式。
在您的车把模板文件中使用这种方式。
<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>
评论handlebar.js的两种方式
单个组件:
{{!fixedTop=true}} --> comments the whole content inside the brackets
多组件:
{{!--fixedTop=true
alignment=true--}} --> comments the whole content until end with "--"
官方网站说:“您可以像在代码中一样在车把代码中使用注释。由于通常存在某种程度的逻辑,因此这是一种很好的做法。
注释不会出现在结果输出中。如果您想显示评论,只需使用 HTML 评论,它们就会被输出。
任何必须包含 }} 或其他把手标记的注释都应使用 {{!-- --}} 语法。”
检查网站 https://handlebarsjs.com/guide/#evaluation-context
使用此代码:
{{#data}}
<!-- enter comments here -->
<p>{{name}}</p>
{{/data}}
不定期副业成功案例分享