如何在“流星空格键"模板中重复N次块? [英] How can I repeat a block N times in a Meteor Spacebars template?
问题描述
我在空格键模板中有以下代码块:
I have this block of code in a Spacebars template:
1.
<select class="form-group">
{{#each choices}}
<option>{{this}}</option>
{{/each}}
</select>
我想重复此N次,每次递增数字,如下所示:
I would like to repeat this N times incrementing the number each time like so:
1.
<select class="form-group">
{{#each choices}}
<option>{{this}}</option>
{{/each}}
</select>
2.
<select class="form-group">
{{#each choices}}
<option>{{this}}</option>
{{/each}}
</select>
3.
<select class="form-group">
{{#each choices}}
<option>{{this}}</option>
{{/each}}
</select>
我希望能够将N传递给自定义模板标签来解决此问题(例如{{choices 3}}
).这样做有什么不错的DRY方法?我有一个模糊的想法,我可以写一个模板助手,但是我不确定从哪里开始.
I would love to be able to pass N to a custom template tag to take care of this (e.g. {{choices 3}}
). What's a nice DRY way to do this? I have a vague notion I could write a template helper, but I'm not sure where to start.
推荐答案
工作示例: http://meteorpad.com/pad/THAQfpfrru5MgAGnS/Copy%20of%20Leaderboard
您可以传递一个计数并返回任意对象的数组.不是最优雅的...但是它起作用了!
You can pass a count in and return an array of arbitrary objects. Not the most elegant... but it worked!
HTML
<body>
{{>content}}
</body>
<template name="content">
{{#each loopCount 5}}
<select class="form-group">
{{#each choices}}
<option>{{this}}</option>
{{/each}}
</select>
{{/each}}
</template>
JS
Template.content.helpers({
choices: function(){
return ['choice1','choice2','choice3']
},
loopCount: function(count){
var countArr = [];
for (var i=0; i<count; i++){
countArr.push({});
}
return countArr;
}
});
这篇关于如何在“流星空格键"模板中重复N次块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!