如何在Ember.js中使用Handlebars脚本进行注释?

28

有谁能告诉我如何在Ember.js的Handlebars模板中包含注释代码?

  <script id="restaurantDetail" data-template-name='restaurantDetail' type="text/x-handlebars">
//Commented code goes here
</script>
2个回答

40

github 页面来看,您想要使用{{! 在这里添加注释文本}}

注释

您可以使用以下语法向模板中添加注释。

{{! This is a comment }}

如果您希望HTML注释最终出现在输出结果中,您也可以使用真实的HTML注释。

<div>
    {{! This comment will not end up in the output }}
    <!-- This comment will show up in the output -->
</div>

1
另外,如果您正在使用真正的HTML注释并且想要包含变量,请确保使用未绑定助手<!-- Item {{unbound itemId}} --> - Ilia Choly
在编程中,注释是否需要在自己的{{}}容器中,或者它们可以是内联的,例如{{name !this is App.controller.content.name}}? - jacobq
我发现如果我放入真正的HTML注释,ember-cli会抱怨它们是“意外”的,并将它们全部吐出到控制台中。 - Epirocks

27

我建议使用{{!-- 在此添加注释 --}},因为这种注释语法可以包含换行符并且在注释中也可以使用}},例如:

Bad comments:
    {{! badly commented {{if somecondition "red" "blue" }} }}  
    {{! badly multiline comments
        another line  }}  

Comment that works:
    {{!-- this is commented correctly {{if somecondition "red" "blue" }} --}}
    {{!-- correct multiline comments
        another line  --}}  

(我知道这是一个老问题,但当在谷歌搜索Ember模板注释时,这个答案会首先出现,所以我想帮助未来的读者)


这绝对是正确的方法,帮助我在学习Ember时受益匪浅(只要确保不在模板中留下注释掉的代码即可 :) ) - Richard Otvos
这可能是在后来的版本中添加的吗?因为被接受的答案是2012年的,而这个是2015年的。 - user2831723

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接