【问题标题】:How Do I Return Template From Meteor Template Helper?如何从 Meteor 模板助手返回模板?
【发布时间】:2014-11-27 22:41:12
【问题描述】:

我的 HTML:

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

如何将一些值返回给 `someTemplateName',以便我可以根据 #each 语句中的对象切换模板。

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

谢谢。

【问题讨论】:

    标签: javascript meteor spacebars meteor-helper


    【解决方案1】:

    正确的语法如下:

    JS

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

    HTML

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

    您操作的并不是真正的名称,而是位于变量名称Template.myTemplate 下的模板对象。

    如果您想操作模板名称,请尝试UI.dynamic:

    HTML

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

    JS

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

    【讨论】:

    • 谢谢!看到你的帖子有点晚了。我导出的实现非常相似。
    【解决方案2】:

    解决方案其实很简单。

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

    我返回一个助手:

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

    this.name 来自 `{{#each}}' 上下文。

    【讨论】:

      猜你喜欢
      • 2013-02-18
      • 2015-01-06
      • 2015-07-28
      • 2016-05-26
      • 2013-05-08
      • 2015-09-20
      • 2015-03-03
      • 2015-12-15
      • 2016-03-04
      相关资源
      最近更新 更多