Nate
Nate

Reputation: 1875

How Do I Return Template From Meteor Template Helper?

My HTML:

<template name="foo">
    {{#each category}}
         {{#if this.custom}}
              {{> someTemplateName}}
         {{else}}
              {{> generic}}
         {{/if}}
     {{/each}}
 </template

How do I return some value to `someTemplateName' so that I can switch templates based on on the object in the #each statement.

 Template.foo.someTemplateName = function () {
      return A_TEMPLATE_NAME
 }

Thanks.

Upvotes: 3

Views: 271

Answers (2)

Nate
Nate

Reputation: 1875

The solution was actually very simple.

 <template name="foo">
     {{#each category}}
        {{#if this.custom}}
          {{> someTemplateName}}
        {{else}}
          {{> generic}}
       {{/if}}
     {{/each}}
 </template>

And I return a helper:

Template.foo.someTemplateName = function () {
    return Template[this.name];
}

Where this.name is from the `{{#each}}' context.

Upvotes: 3

saimeunt
saimeunt

Reputation: 22696

The correct syntax is the following :

JS

Template.foo.helpers({
  someTemplate:function () {
    return Template.someTemplate;
  }
});

HTML

<template name="someTemplate">
  <p>SOME TEMPLATE</p>
</template>

It is not really the name that you manipulate but template objects which live under the variable name Template.myTemplate.

If you want to manipulate template names, try UI.dynamic :

HTML

<template name="foo">
  {{> UI.dynamic template=someTemplateName}}
</template>

JS

Template.foo.helpers({
  someTemplateName:function () {
    return "someTemplate";
  }
});

Upvotes: 3

Related Questions