【问题标题】:How to use an array to render dynamically multiple templates in Meteor?如何使用数组在 Meteor 中动态渲染多个模板?
【发布时间】:2016-03-18 18:06:03
【问题描述】:

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

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>

我确信这相当简单,但我只需要正确的语法,这样我就可以使用来自帮助程序的数据作为要呈现的模板的名称。

【问题讨论】:

    标签: javascript meteor meteor-blaze spacebars meteor-helper


    【解决方案1】:

    您可以使用 {{&gt; 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-26
      • 1970-01-01
      • 1970-01-01
      • 2013-04-07
      • 2014-12-08
      • 2014-12-12
      • 2012-06-13
      相关资源
      最近更新 更多