I haven't been able to find a solid example out there.
Template.registerHelper("itemLookup", function(sku, property){
return Items.findOne({sku: sku})[property];
});
How do I call this on the template?
I want to do something like:
{{ itemLookup sku="i3_4030U" property="title" }}
It should output
"Intel Core i3 4030U"
Do not name template helpers parameters, they will be passed in the same order to your helper function :
{{ itemLookup "i3_4030U" "title" }}
EDIT :
Why then do I see examples online where they are naming the template helper parameters?
You can name parameters when including another template and you want to set its current data context to something else :
{{> childTemplate param1="A" param2="B"}}
In the child template markup you'll be able to reference {{param1}}
and {{param2}}
.
Another Handlebars helpers feature available in Spacebars is the "hash" optional argument value you can pass as the last argument to your helper parameters, you can use it like this :
HTML
{{helper "A" "B" namedParam1="C" namedParam2="D"}}
JS
Template.registerHelper("helper", function(param1, param2, options){
console.log("param1 :", param1);
console.log("param2 :", param2);
if(options && options.hash){
console.log("namedParam1 :", options.hash.namedParam1);
console.log("namedParam2 :", options.hash.namedParam2);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With