Tobias
Tobias

Reputation: 43

Dynamic templates in Meteor

I have a question about dynamic templates in Meteor. I want to use a template multiple times with other data, but have no idea what I am doing wrong. I have the following template:

<template name="knowyourcompany">
    <section>
        <header>
            <h2>Know your company</h2>
        </header>
        <p class="question">Q: {{question}}</p>
    </section>
</template>

I have the following template helper:

Template.knowyourcompany.helpers({
    knowyourcompany1: function(){
        return { question: "test" }
    }
});

And this is how I include the template

{{> Template.dynamic template="knowyourcompany" data=knowyourcompany1}}

The problem is that I see an empty template. What am I doing wrong?

Upvotes: 1

Views: 237

Answers (1)

Peppe L-G
Peppe L-G

Reputation: 8345

Template.dynamic should be used if you want the template to be interchangeable, but in your case you want your data context to be interchangeable, so this is no use case for Template.dynamic. Instead, use the ordinary {{> templateName myDataContext}}:

<template name="myMainTemplate">
    {{> myTemplate myDataContext}}
</template>
Template.myMainTemplate.helpers({
    myDataContext: function(){
        return {a: 1, b: 2}
    }
})
<template name="myTemplate">
    a: {{a}}, b: {{b}}
</template>

Upvotes: 1

Related Questions