【问题标题】:Passing a parameter into Meteor Template Helper将参数传递给 Meteor 模板助手
【发布时间】:2015-09-02 13:25:33
【问题描述】:

我正在尝试将参数传递给模板助手。该参数似乎已传递,但我无法按照我的意愿使用它。

我这样传递参数:

{{#if unitFieldExists 'profile'}}
  {{profile}}
{{/}}

/client/lib/helpers.js 中的帮助程序

Template.registerHelper('unitFieldExists', function(unitField) {
var doc = Units.findOne({_id: this._id }, { unitField : {$exists: true} });

// console tests
console.log(unitField); // Outputs profile
console.log(doc); // Outputs document object

// unitfield if of type string
console.log(typeof unitField + " " + unitField); // string

if (doc.unitField) {
    return true;
} else {
    return false;
}
});

如果文档包含传递的字段,我想要实现的是返回 true 到 #if。我可能把事情复杂化了,但我仍在学习。

【问题讨论】:

    标签: meteor spacebars


    【解决方案1】:

    您不能在 #if 中调用带有参数的助手。

    我假设模板实例的数据上下文是一个Unit 文档,因为您使用了this._id。如果是这样,可以这样做;

    {{#if profile}}
      {{profile}}
    {{/if}}
    

    #if 检查其参数是否为真。

    【讨论】:

    • 哦,那太尴尬了……复杂了几千倍。效果很好,谢谢!
    • 没问题。请随意接受我的回答,这样我就可以获得一些互联网积分。
    猜你喜欢
    • 2015-07-10
    • 2015-09-30
    • 2015-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-07
    • 2018-05-12
    • 1970-01-01
    相关资源
    最近更新 更多