[英]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.