【问题标题】:Meteor data context, passing array into each Spacebars loop流星数据上下文,将数组传递到每个空格键循环
【发布时间】:2015-02-25 05:51:45
【问题描述】:

在这个Example 2——“传递数据上下文

JS

Template.overview.helpers({  
  users: [
    { name: 'David' },
    { name: 'Shaune' }
  ]
});

HTML

<template name="overview">  
  {{> userList users}}
</template>

<template name="userList">  
  {{#each this}}
    {{name}}<br>
  {{/each}}
</template>  

结果

David  
Shaune 

我的目标是将一组模板名称传递给用户所在的Template.overview,如下所示:

<template name="overview">  
  {{#each templateArray }}
    {{> userList this }}
  {{/each}}
</template>

其中templateArray 是带有模板名称的字符串数组,this 是每个模板的名称。

这可能吗?

编辑:

如需更深入,请考虑我的helpers 中有以下内容

Template.overview.helpers({  
  users: [
    { name: 'David' },
    { name: 'Shaune' }
  ],
  otherUsers: [
    { name: 'Jane' },
    { name: 'Betty' }
  ],
  moarUsers: [
    { name: 'Ben' },
    { name: 'Jerry' }
  ]
});

我想将这些数据上下文中的每一个传递到同一个模板中(在本例中为userLists)。

【问题讨论】:

    标签: javascript meteor handlebars.js spacebars


    【解决方案1】:

    你不需要调用任何东西,你可以在helper里面使用this,比如

    helper: function(){
       return this; //if you pass string via templateArray
       return this.name; //if you pass object via templateArray
    }
    

    因为你所做的看起来像这样,但只是很好地包装在模板中

    <template name="overview">  
      {{#each templateArray }}
          {{name}}<br>
      {{/each}}
    </template>
    

    我不知道你为什么在代码双循环中,在第一个循环中你传递对象并且它不能迭代它

    编辑

    如果您只想循环遍历字符串数组,请执行以下操作:

    {{#each templateArray}}
           {{this}}<br>
     {{/each}}
    

    【讨论】:

    • 目标是渲染许多模板(因为它们在每种情况下都有唯一的数据上下文),因此向 {{> userList }} 提供参数是我对此的假设。用户助手是一个对象数组,我有很多这样的数组。
    • 但是您想为他们提供他们已经拥有的this,在上面的示例中,您可以在 userList 模板代码中的任何位置使用 this.name 或在 html 中的任何位置使用 {{name}}
    猜你喜欢
    • 2023-03-24
    • 1970-01-01
    • 2014-11-24
    • 2016-03-07
    • 2020-12-17
    • 1970-01-01
    • 2015-06-11
    • 2016-07-14
    • 2016-03-18
    相关资源
    最近更新 更多