javascript - Use a variable as name to access nested elements of another array -
i each-loop array build in helper:
template.article.helpers({ section: function() { return [ {type: 'cars', sectiontitle: 'cars'}, {type: 'vegetables', sectiontitle: 'vegetables'} ]; }, }); the data articles comes router:
router.route('/article/:_id', { name: 'article', data: function() { return { article: articles.findone({ _id: this.params._id }) } } }); but want access subelement of article type of helper. in example each loop done 2 times: first want use ../article.cars , ../article.vegetable. hope understand problem. want name of subelement helper type:
<template name="article"> {{#each section}} <h1>{{this.sectiontitle}}</h1> <ul> {{#each ../article.type}} <!-- should '../article.cars' , '../article.vegetable' --> <li>{{this.title}}</li> {{/each}} </ul> {{/each}} </template> i want use content of type variable name. if type 'cars', want use ../articles.cars'. likearticles['cars']which result ofarticles[type]. in meteor writing not possible. andarticles.type` different.
just use helper:
s: function(article) { return article[this.type]; } and send argument spacebar:
{{#each s ../article}}
Comments
Post a Comment