@DuanPengfei
2015-02-09T23:08:39.000000Z
字数 726
阅读 1558
Meteor 中,视图定义在模板中。一个模板是可以包含动态数据的一小段 HTML 代码。可以通过 JavaScript 代码向模板中添加数据和监听事件。
模板是在 .html 文件中定义的,可以放在 Meteor 项目目录下除 server/,public/,private/ 文件夹之外的任何地方。
每个 .html 文件可以包含任何数量的顶级元素:<head>
,<body>
和 <template>
。包含在 <head>
和 <body>
标签中的代码将被添加到 HTML 代码中得相应位置,包含在 <template>
标签中得代码可以像下面的例子一样,使用 {{> templateName}}
引入。模板可以引入多次——模板的主要目的之一是为了避免多次编写相同的 HTML 代码。
<!-- add code to the <head> of the page -->
<head>
<title>My website!</title>
</head>
<!-- add code to the <body> of the page -->
<body>
<h1>Hello!</h1>
{{> welcomePage}}
</body>
<!-- define a template called welcomePage -->
<template name="welcomePage">
<p>Welcome to my website!</p>
</template>
{{ ... }} 语法是 Spacebars 语言的一部分,Meteor 用来向 HTML 添加功能。如上所示,它可以在页面的其他部分引入模板。使用 Spacebars,还可以显示来自 helpers 的数据。Helpers 是用 JavaScript 编写的,可以是简单的值或方法。