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

How to use an array to render dynamically multiple templates in Meteor?

本文关键字:动态 数组 Meteor      更新时间:2023-09-26

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

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>

我相信这相当容易,但我只需要正确的语法,这样我就可以使用来自helper的数据作为要渲染的模板的名称。

您可以使用{{> 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。