Search code examples
javascriptmeteorspacebarsmeteor-helper

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.


Solution

  • 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.