简体   繁体   English

如何嵌套空格键标签?

[英]How Do I Nest Spacebars Tags?

I have a problem. 我有个问题。 I have a collection of documents in my database, and I would like to display each document depending on whether a session variable corresponding to that document is set. 我的数据库中有一个文档集合,我想根据是否设置了与该文档相对应的会话变量来显示每个文档。

To illustrate my problem, let me give a simplified example. 为了说明我的问题,让我举一个简化的例子。

Meteor.myTemplate.helpers({
  stuff: [
    {name: 'item1'},
    {name: 'item2'}
    {name, 'item3'},
    {name: 'item4'},
    {name: 'item5'}
  ],
  isActive: function (itemName) {
    return Session.get(itemName + 'IsActive');
  }
});

Now, what I would like to do is this: 现在,我想做的是:

{{#each stuff}}
    {{#if isActive {{name}} }}
      {{> someTemplate}}
    {{/if}}
{{/each}}

But since nested tags like this are not supported, I can't do this. 但是由于不支持这样的嵌套标签,因此无法执行此操作。 I am looking for a way to achieve this without having to write: 我正在寻找一种无需编写以下代码的方法:

{{if isActive 'item1'}}{{> someTemplate}}{{/if}}
{{if isActive 'item2'}}{{> someTemplate}}{{/if}}
{{if isActive 'item3'}}{{> someTemplate}}{{/if}}
{{if isActive 'item4'}}{{> someTemplate}}{{/if}}
{{if isActive 'item5'}}{{> someTemplate}}{{/if}}

Which is possible if the data is static like in the above example (instead of coming from a database cursor), but it is not really practical in any case, especially with a lot of items. 如果数据像上面的示例一样是静态的(而不是来自数据库游标),则可以这样做,但是在任何情况下(特别是对于很多项目)都不是切实可行的。 Furthermore, this style probably makes every programmer cringe. 此外,这种风格可能会使每个程序员都畏缩。

Since the context of isActive is a stuff , then this.name should be available to the helper: 由于isActive的上下文是一个stuff ,因此this.name应该对帮助者可用:

Template.myTemplate.helpers({
  isActive: function(itemName) {
    return Session.get(this.name + 'IsActive');
  }
});

Your template would look like: 您的模板如下所示:

{{#each stuff}}
  {{#if isActive}}
    {{> someTemplate}}
  {{/if}}
{{/each}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何注册流星空格键助手? - How do I Register Meteor Spacebars Helpers? 使用空格键时如何检查匹配的字符串? - How Do I Check For Matching Strings When Using Spacebars? 如何在空格键中使用动态对象属性名称? - How Do I Use Dynamic Object Property Names in Spacebars? 如何在空格键{{if}}中获取HTML以使用Session变量进行反应式更改? - How do I get HTML inside Spacebars {{if}} to change reactively with a Session variable? 如何在空格键#each标签中显示来自两个不同集合文档的数据? - How Do I Display Data From Two Different Collection's Documents in Spacebars #each Tag? 使用Meteor时如何正确设置嵌套的#each空格键迭代器的范围? - How Do I Properly Scope Nested #each Spacebars Iterators When Using Meteor? 如何使用流星空格键{{#each}}块遍历字符串数组? - How do I iterate over an array of Strings with the Meteor Spacebars {{#each}} block? 如何使用空格键模板助手来防止不需要不必要的模板? - How Do I Use Spacebars Template Helpers to Prevent Need For Unnecessary Templates? 如何渲染带有流星空格键模板的表和每行的行号? - How Do I Render Table With Meteor Spacebars Template and Row Number For Each Row? 呈现模板后,如何在空格键中更改模板助手功能? - How do I change template helper function in Spacebars after template has been rendered?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM