【问题标题】:Using {[#custom}} contents {{/custom}} in Meteor在 Meteor 中使用 {[#custom}} 内容 {{/custom}}
【发布时间】:2015-12-30 00:40:02
【问题描述】:

我刚刚开始为 Meteor 使用 templates:tabs package。默认情况下,这会生成一个作为{{#basicTabs tabs=tabs}} 插入的自定义模板。

我制作了一个简单的应用程序来了解这种自定义模板的工作原理,特别是了解这与使用 {{> custom}} 有何不同

HTML:

<body>
  {{> parent1}}
  {{> parent2}}
</body>

<template name="parent1">
  <h1>Parent 1</h1>
  {{> child}}
</template>

<template name="parent2">
  <h1>Parent 2</h1>
  {{#child}}
    <h2>Stuff inside "child" tag</h2>
  {{/child}}
</template>

<template name="child">
  <button type="button">Child button</button>
</template>

JS:

if (Meteor.isClient) {
  Template.child.events({
    'click button': function (event, template) {
       console.log("child", event, template)
    }
  });
  Template.parent1.events({
    'click button': function (event, template) {
      console.log("parent1", event, template)
    }
  });
  Template.parent2.events({
    'click button': function (event, template) {
      console.log("parent2", event, template)
    }
  });
}

我没有看到任何 Stuff inside the "child" tag 出现。当我点击一个按钮时,我可以在浏览器控制台中看到父模板和子模板都可以对输入做出反应,但这两种情况都会发生。

我应该如何使用{{#custom}} ... {{/custom}} 语法?


编辑

以下是显示 {{#child}} 块如何工作的更新文件,与 events 和 helpers 相关:

<body>
  {{> parent1}}
  {{> parent2}}
</body>

<template name="parent1">
  <h1>Parent 1</h1>
  {{> child}}
</template>

<template name="parent2">
  <h1>Parent 2</h1>
  {{#child}}
    <h2>Stuff inside "child" tag</h2>
  {{else}}
    <h3>Other stuff</h3>
  {{/child}}
</template>

<template name="child">
  {{extras}}
  {{> Template.contentBlock}}
  {{> Template.elseBlock}}
  <button type="button">Child button</button>
</template>

JS:

if (Meteor.isClient) {
  Template.child.events({
    'click button': function (event, template) {
       console.log("child", event, template)
    }
  });
  Template.parent1.events({
    'click button': function (event, template) {
      console.log("parent1", event, template)
    }
  });
  Template.parent2.events({
    'click button': function (event, template) {
      console.log("parent2", event, template)
    }
  });

  Template.child.helpers({
    extras: function () {
      return "Child extras"
    }
  })
  Template.parent1.helpers({
    extras: function () {
      return "Parent 1 extras"
    }
  })
  Template.parent2.helpers({
    extras: function () {
      return "Parent 2 extras"
    }
  })
}

使用 Meteor 1.2.0.2 输出:

父母 1

儿童附加功能 [儿童按钮]

父母 2

儿童剧

“子”标签内的东西

其他东西

[子按钮]

【问题讨论】:

    标签: templates meteor


    【解决方案1】:

    看来你这里唯一缺少的就是Template.contentBlock,即

    <template name="child">
      <button type="button">Child button</button>
      {{> Template.contentBlock}}
    </template>
    

    您可以将其视为一个助手,能够呈现child 模板的用户在{{#child}}..{{/child}} 块中放置的任何内容。

    回到你的问题,{{&gt; child}} 和{{#child}} 之间的主要区别在于前者的Template.contentBlock 等于null。供您参考,还有另一个帮助器Template.elseContentBlock 表示放置在{{else}}“标签”之后的标记部分,例如

    {{#child}}
       this content goes to Template.contentBlock
    {{else}}
       this content goes to Template.elseContentBlock
    {{/child}}
    

    您可以使用它让您的child 模板根据某些上下文选择要呈现的内容,如示例here 所示。

    在您的问题中没有直接指出,但要记住的另一件事是,如果您使用块语法 {{#child}},那么块的内容可以访问您的 parent 模板的帮助程序,并且无权访问child 模板的助手。

    【讨论】:

    • “块的内容可以访问你的父模板的助手,但不能访问子模板的助手”——我没有看到这个。我在我的问题中添加了新的 HTML 和 JS 以进行测试:我看到子助手正在创建“Child extras”。
    猜你喜欢
    • 2015-06-23
    • 2018-05-26
    • 2019-08-11
    • 2023-01-12
    • 2015-12-02
    • 2011-04-26
    • 2022-12-27
    • 1970-01-01
    • 2021-11-09
    相关资源
    最近更新 更多