Meteor - 模板(Template)

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

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

简单模板

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

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

meteorApp.html

链接:https://www.learnfk.comhttps://www.learnfk.com/meteor/meteor-templates.html

来源:LearnFk无涯教程网

<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

链接:https://www.learnfk.comhttps://www.learnfk.com/meteor/meteor-templates.html

来源:LearnFk无涯教程网

<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

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

技术教程推荐

朱赟的技术管理课 -〔朱赟〕

SQL必知必会 -〔陈旸〕

Linux实战技能100讲 -〔尹会生〕

跟月影学可视化 -〔月影〕

人人都用得上的写作课 -〔涵柏〕

Python自动化办公实战课 -〔尹会生〕

全链路压测实战30讲 -〔高楼〕

朱涛 · Kotlin编程第一课 -〔朱涛〕

结构思考力 · 透过结构看问题解决 -〔李忠秋〕

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