javascript - 如何在 Meteor 中使用数组动态渲染多个模板?

标签 javascript meteor meteor-blaze spacebars meteor-helper

我在使用助手中的数据呈现模板时遇到问题。

Template.bonus.helpers({
    Userform: function(){
      return UserForms.find({owner:Meteor.userId()});
    },
    warehouse: function(){
      return Warehouse.find({});
    },
    DisplayForm: [
      { formname: "SW - Picking" },
      { formname: "SW - Packing" },
      { formname: "SW - Putaway" }
    ]
  });

基本上我只想实现这样的目标:

<div id="placeholder" class="col-md-8">
    {{#each DisplayForm}}
        {{> {{formname}} }}       //Render 3 templates "SW - Picking","SW - ....
    {{/each}}
</div>

我相信这很容易,但我只需要正确的语法,这样我就可以使用来自助手的数据作为要呈现的模板的名称。

最佳答案

您可以使用 {{> Template.dynamic template=template [data=data] }} 在Meteor 中动态包含一个模板。 .

例如:

<body>
  {{> bonus}}
</body>

<template name="bonus">
  {{#each displayForm}}
    {{> Template.dynamic template=formname }}
    <br />
  {{/each}}
</template>

<template name="picking">
  Picking Template
</template>

<template name="packing">
  Packing Template
</template>

<template name="putaway">
  Putaway Template
</template>

if (Meteor.isClient) {
  Template.bonus.helpers({
    displayForm: [{
      formname: "picking"
    }, {
      formname: "packing"
    }, {
      formname: "putaway"
    }]
  });
}

这是一个MeteorPad .

关于javascript - 如何在 Meteor 中使用数组动态渲染多个模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34247050/

相关文章:

node.js - 使用 Meteor (CFS) 下载远程图像

javascript - Meteor.js 中的房间/ channel 和 userId

javascript - Meteor.js Summernote 编辑器。从数据库 MongoDB 读取数据

javascript - Meteor 检索嵌套数组

javascript - 如何使用 Meteor 获取更多数据并附加到文档

javascript - 发送 Ajax 请求之前的事件(向请求添加一些数据)

javascript - TypeScript包含@types而不包含JS库的原理是什么?

MongoDB:查找匹配最多标签的文档

javascript - 将数组内部的数组解析为 PHP

javascript - 如何在 promise.then () 中检索对此的引用?