简体   繁体   中英

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.

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";
  }
});

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM