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

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

来源:LearnFk无涯教程网

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

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

来源:LearnFk无涯教程网

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

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

技术教程推荐

机器学习40讲 -〔王天一〕

从0开发一款iOS App -〔朱德权〕

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

小马哥讲Spring核心编程思想 -〔小马哥〕

系统性能调优必知必会 -〔陶辉〕

A/B测试从0到1 -〔张博伟〕

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

人人都用得上的数字化思维课 -〔付晓岩〕

Midjourney入门实践课 -〔Jovi〕

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