
视图模板——使用
Handlebars
|
69
7.4.1
注释
Handlebars
中的
注释
是这样的:
{{! comment goes here }}
。理解
Handlebars
注释和
HTML
注释的区别很重要。考虑下面的模板:
{{! super-secret comment }}
<!-- not-so-secret comment -->
假设这是服务器端模板,
super-secret
这条注释是绝对不会发送到浏览器的,而如果用户查
看
HTML
源代码
,就会看到
not-so-secret
这条注释。对于任何会暴露实现细节或不想公开
的地方,都应该选择
Handlebars
注释。
7.4.2
代码块
当你考虑
代码块
的时候,事情就开始变得复杂了。代码块提供了流程控制、条件执行和扩
展性。考虑下面的上下文对象:
{
currency: {
name: 'United States dollars',
abbrev: 'USD',
},
tours: [
{ name: 'Hood River', price: '$99.95' },
{ name: 'Oregon Coast', price: '$159.95' },
],
specialsUrl: '/january-specials',
currencies: [ 'USD', 'GBP', 'BTC' ],
}
把这个上下文传入下面的模板:
<ul>
{{#each tours}}
{{! 现在在一个新的代码块里了,上下文已经改变了 ...