{{#each category}} {{#if this.custom}} {{> someT...">

How do I get a template back from the Meteor template helper?

My HTML:

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

      

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

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

      

Thank.

+3


source to share


2 answers


The solution was actually very simple.

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

      

And I return the helper:



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

      

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

+2


source


The correct syntax is as follows:

Js

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

      

Html

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

      

It is not actually a name that you control, but template objects that live under the variable name Template.myTemplate

.



If you want to control template names try UI.dynamic

:

Html

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

      

Js

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

      

+2


source







All Articles