将参数传递给 Meteor Template Helper

Passing a parameter into Meteor Template Helper

我正在尝试将参数传递给模板助手。参数好像传过去了,但是我想怎么用都用不了

我这样传递参数:

{{#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;
}
});

如果文档包含传递的字段,我想要实现的是 return #if 为真。我可能把事情复杂化了,但我还在学习。

您不能在 #if.

中调用带参数的助手

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

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

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