Meteor模板

Meteor模板使用三个顶级标签。前两个是 head 和 body 标签。这些标签和在普通的HTML中做的工作一样。第三个标签 template。这是我们将HTML连接到JavaScript的地方。

 

简单的模板

下面的例子显示了这一过程。我们使用 name = "myParagraph"属性创建一个模板。我们的 template 标签body元素下方创建,但需要包括它在屏幕渲染显示之前。我们也可以使用 {{> myParagraph}} 语法. 在模板中我们使用的是双大括号 ({{text}}). 这就是所谓的 meteor 模板Spacebars 语言。

在 JavaScript文件我们设置 Template.myParagraph.helpers({}) 方法是对模板连接。我们只在本示例中使用 text 助手。

meteorApp/client/import/ui/first-tpl.html

<head>
   <title>meteorApp</title>
</head>
 
<body>
   <h1>Header</h1>
   {{> myParagraph}}
</body>
 
<template name = "myParagraph">
   <p>{{text}}</p>
</template>

在 JavaScript文件我们设置 Template.myParagraph.helpers({}) 方法是对模板连接。我们只在本示例中使用 text 助手。

meteorApp/client/main.js

import { Template } from 'meteor/templating';
Template.myParagraph.helpers({
  text: 'This is paragraph...'
});
我们保存更改之后,打开浏览器会得到下面的输出 -

 

块模板

在这个例子中,我们使用的是 {{#each paragraphs}} 遍历数组 paragraphs,并返回模板 name = "paragraph" 遍历每个值 。

 

meteorApp/client/import/ui/first-tpl.html

<head>
   <title>meteorApp</title>
</head>
 
<body>
   <div>
      {{#each paragraphs}}
         {{> paragraph}}
      {{/each}}
   </div>
</body>
 
<template name = "paragraph">
   <p>{{text}}</p>
</template>

这里我们需要创建 paragraphs 助手. 这是有五个文本值的数组。

 

meteorApp/client/main.js

// This code only runs on the client
import { Template } from 'meteor/templating';
Template.body.helpers({
  paragraphs: [
     { text: "This is paragraph 1..." },
     { text: "This is paragraph 2..." },
     { text: "This is paragraph 3..." },
     { text: "This is paragraph 4..." },
     { text: "This is paragraph 5..." }
  ]
});
现在我们可以在屏幕上看到五个段落。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值