【问题标题】:Meteor.js using value of a helper as templateMeteor.js 使用帮助器的值作为模板
【发布时间】:2016-01-06 08:30:34
【问题描述】:

我想根据辅助值呈现不同的模板。我将尝试编写和示例。

...
{{#with myHelper}}
 {{> this }}
{{/with}}
...

使用 helper 定义如下:

...
myHelper : function(){
  return MyCollection.findOne({ userId: Meteor.userId() }).personalizeTemplate; 
}
...

不幸的是,这种方式不起作用。有什么解决办法吗?

【问题讨论】:

    标签: javascript meteor handlebars.js spacebars


    【解决方案1】:

    要动态使用模板,您必须使用全局 Template.dynamic

    {{> Template.dynamic template="my template"}}
    
    {{> Template.dynamic template=myHelper}}
    

    注意myHelper 必须返回一个字符串,它是模板的名称。
    您还可以使用data 提供数据上下文:

    {{> Template.dynamic template=myTemplate data=someData}}
    

    Discover Meteor 写了一篇关于它的文章。它创建于 2014 年,整篇文章中使用的 UI 命名空间已重命名为 Template

    【讨论】:

      【解决方案2】:

      你需要使用dynamic template 你甚至不需要{{#with}}

      {{> Template.dynamic template=myHelper }}
      

      【讨论】:

      • 非常感谢...这正是我所需要的
      猜你喜欢
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-15
      • 2014-12-03
      • 1970-01-01
      • 2013-12-06
      • 1970-01-01
      相关资源
      最近更新 更多