Nick
Nick

Reputation: 12037

How to alternate between several templates in Meteor?

My meteor app has a base.html file like this:

<head>
  <title>MyApp</title>
</head>

<body>
    <template name="bodyContent">
        {{subContent}}
    </template>
</body>

Next, I define three different templates:

<template name="templateA">Some text</template>
<template name="templateB">Bla bla bla</template>
<template name="templateC">Your highscore is {{score}}</template>

How can I make {{subContent}} so it is able to alternate between one of these three templates at runtime?

Something like:

currentTemplateUsed = 'templateB'

Template.bodyContent.subContent = Template[currentTemplateUsed]

But that doesn't work. Are there other approaches to achieve this functionality?

Upvotes: 3

Views: 497

Answers (2)

Nick
Nick

Reputation: 12037

You should use meteor router, it is able to dynamically change templates. Possible downside is that it can only change one template at a time.

Alternatively, there's a bit of info on Meteor's Handlebars: https://github.com/meteor/meteor/wiki/Handlebars

Basically, you have to use the template as a function, and pass data to it in order for Handlebars to parse it, like this:

var currentTemplateUsed = 'templateC';

var data = {
   score : 12
}

Template.bodyContent.subContent = Template[currentTemplateUsed](data);

Returns Your highscore is 12

Upvotes: 2

esp
esp

Reputation: 7717

Templates inside of <body> ... </body> don't seem to work, I couldn't pass data inside. I might be wrong, but I had to put it outside and use:

<body>
  {{> body_content}}
</body>

Upvotes: 0

Related Questions