1

I do a each-loop for an array which I build in a helper:

Template.article.helpers({
    section: function() {
        return [
            {type: 'cars', sectionTitle: 'Cars'}, 
            {type: 'vegetables', sectionTitle: 'Vegetables'}
        ];
    },
});

The data for the articles comes from the router:

Router.route('/article/:_id', {
    name: 'article',
    data: function() {
        return {
            article: Articles.findOne({ _id: this.params._id })
        }
    }   
});

But now I want to access a subelement of article with the type of the helper. So in this example the each loop will be done two times: I first want to use ../article.cars and then ../article.vegetable. I hope you understand my problem. I want to get the name of the subelement by the helper type:

<template name="article">
    {{#each section}}
        <h1>{{this.sectionTitle}}</h1>

        <ul>
        {{#each ../article.type}} <!-- should get '../article.cars' and '../article.vegetable' -->
            <li>{{this.title}}</li>
        {{/each}}
        </ul>
    {{/each}}
</template>

I want to use the content of type as a variable name. If type is 'cars', then I want to use ../articles.cars'. Which would be something likearticles['cars']which would result ofarticles[type]. But in meteor this writing is not possible. Andarticles.type` is something different.

2
  • This looks like it is specific to your view engine. What view engine is this? Commented Oct 3, 2015 at 7:30
  • @baao I think I need something like obj[name], but in meteor (see tags) there are dot notation used. But article.type is looking for the field type. But I want to use the content of type as name. Commented Oct 3, 2015 at 7:40

1 Answer 1

2

Just use another helper:

s: function(article) {
    return article[this.type];
}

And send a argument with your spacebar:

{{#each s ../article}}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.