A_L
A_L

Reputation: 1146

Pass a template name as string to another template in Meteor/Handlebars

I have a template that contains a second template, I'd like to make the second template variable ie. it can change depending on what I pass as the template name.

<template name="template1">
    <div>
        {{> template2}}
    </div>
</template>

So when my main page shows template1 I want to specify the template to use for template2.

<template name="main">
    {{> template1 TemplateArgs }}
</template>

I know how to set up TemplateArgs - it would contain the name of the template I want to use for template2.

Is this possible?

Upvotes: 1

Views: 385

Answers (1)

Tarang
Tarang

Reputation: 75945

A future version of meteor allows this with the new Meteor UI functionality. Currently you need to do this manually in a different way. See Meteor set overall template context.

Meteor UI

Beware meteor UI is still in a betaish and is a bit buggy. More details on the post in the references

If you use meteor UI (which you can use in preview mode with)

meteor --release template-engine-preview-5

Then you can do stuff like this

Template.main.template1 = function (value2) {
    return Template["template2"].withData({foo: "bar", value2: value2}));
}

Then html like

{{>template1 "valueofvalue2"}}

Current Version / Spark rendering

There is a way in the current version of meteor but it wont work with the UI release since Meteor.render will be phased out in favour of using DOM objects

Template.main.template1 = function(value2) {
    return Meteor.render(Template.template2({foo: "bar", value2: value2});
}

Then something like this in your html. You might have to play around with the data to get it working but its something like this

{{template1 "value2"}}

References:

Upvotes: 1

Related Questions