Meteor - 模板(Template)

Meteor - 模板(Template) 首页 / Meteor入门教程 / Meteor - 模板(Template)

Meteor模板正在使用三个顶级标签。前两个是 headbody 。这些标签执行与常规HTML相同的功能,第三个标签是 template 。这是无涯教程将HTML连接到JavaScript的地方。

简单模板

以下示例显示了它是如何工作的。无涯教程正在创建一个name=“ myParagraph”属性的模板,无涯教程可以使用{{> myParagraph}}语法来做到这一点。变量通过花括号({{text}})进行渲染。

在无涯教程的JavaScript文件中,无涯教程设置 Template.myParagraph.helpers({})方法,这将是无涯教程与模板的连接。

meteorApp.html

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

meteorApp.js

if (Meteor.isClient) {
   
   //This code only runs on the client
   Template.myParagraph.helpers({
      text: 'This is paragraph...'
   });
}

保存更改后,以下是输出-

Meteor Templates Output

Block模板

在下面的示例中,无涯教程使用 {{#each每个段落}} 遍历 paragraphs 数组并返回模板 name =" paragraph" 每个值。

meteorApp.html

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

无涯教程需要创建 paragraphs 助手,这是一个具有五个text的数组。

无涯教程网

meteorApp.js

if (Meteor.isClient) {
   
   //This code only runs on the client
   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..." }
      ]
   });
}

现在,无涯教程可以在屏幕上看到五个段落。

Meteor Templates Output 2

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

从0开始学游戏开发 -〔蔡能〕

技术管理实战36讲 -〔刘建国〕

许式伟的架构课 -〔许式伟〕

Kafka核心技术与实战 -〔胡夕〕

Vim 实用技巧必知必会 -〔吴咏炜〕

乔新亮的CTO成长复盘 -〔乔新亮〕

如何成为学习高手 -〔高冷冷〕

零基础实战机器学习 -〔黄佳〕

结构思考力 · 透过结构看表达 -〔李忠秋〕

好记忆不如烂笔头。留下您的足迹吧 :)