【问题标题】:Template Helper Functions模板帮助函数
【发布时间】:2015-05-09 06:52:17
【问题描述】:

是否可以将变量传递给模板辅助函数以确定返回哪个对象,而无需为每个对象创建新模板?大概是这样的吧?

{{#each frame}}
    <p>{{name}}</p>
    <p>{{description}}</p>
{{/each}}


Template.templateName.helpers({
classic : [
   {
    name : 'first option',
    description : 'this is option 1'
   },
   {
    name : 'second option',
    description : 'this is option 2'
   }],
versions : [
   {
    name : 'first option',
    description : 'this is option 1'
   },
   {
    name : 'second option',
    description : 'this is option 2'
   }],
{
'frame' : function(boardSpecs){
    if (boardSpecs[0] == "classic"){
        return classic;
    }
    else if (boardSpecs[0] == "versions"){
        return versions;
    }
}
})

【问题讨论】:

  • 您可以直接在模板中指定上下文数据:{{> yourTpl name='first option' description='some description'}}

标签: templates meteor helpers


【解决方案1】:

你可能可以玩 {{#if}}。

仅以经典和版本为例:

模板:

{{#each frame}}
    {{#if classic.active}}
        <p>{{classic.name}}</p>
        <p>{{classic.description}}</p>
    {{/if}}
    {{#if versions.active}}
        <p>{{versions.name}}</p>
        <p>{{versions.description}}</p>
    {{/if}}
{{/each}}

助手:

Template.templateName.helpers({
    active: {
        classic: function(){ return Session.get("active") == "classic"; }
        versions: function(){ return Session.get("active") == "versions"; }
    },
    classic : [
       {
           name : 'first option',
           description : 'this is option 1'
       },
       {
           name : 'second option',
           description : 'this is option 2'
       }],
     versions : [
       {
           name : 'first option',
           description : 'this is option 1'
       },
       {
           name : 'second option',
           description : 'this is option 2'
       }]
    })

然后你可以在需要换视图的时候设置Session.set("active", "name of the one you want")。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-26
    • 1970-01-01
    • 2011-01-20
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    • 2016-07-06
    相关资源
    最近更新 更多