handlebars.js - Meteor template: Pass a parameter into each sub template, and retrieve it in the sub-template helper -
i trying figure out how pass parameter sub-template in each block , use parameter in sub-template sub-template helper. here tried far:
template:
<template name="parent"> {{#each nodes }} {{> child myparam}} {{/each}} </template> <template name="child"> {{ paramname }} </template>
js:
template.parent.nodes = function() { //return list }; template.parent.myparam = function() { return {"paramname" : "paramvalue"}; }; template.child.someotherhelper = function() { //how access "paramname" parameter? }
so far, hasn't been working, , seems somehow mess input node list also.
help.
when use {{> child myparam}}
, it's calling child template , associates myparam
current template data context, meaning in template can reference {{paramname}}
.
in someotherhelper
use this.paramname
retrieve "paramvalue"
. however, when you're using {{#each nodes}}{{> child}}{{/each}}
, means pass content of current list item (fetched localcursor
or directly array item) template data of child, , can reference list item properties using {{field}}
in html or this.field
in js.
what's happening here when call {{> child myparam}}
, myparam
helper content overwrites current node item template data, that's why it's messing node list.
a quick (dirty) trick extend myparam
helper contains template data {{#each}}
block.
template.parent.helpers({ nodes:function(){ // simulate typical collection cursor fetch result return [{_id:"a"},{_id:"b"},{_id:"c"}]; }, myparam:function(){ // here, equals current node item // _.extend our param return _.extend({paramname:"paramvalue"},this); } }); template.child.helpers({ someotherhelper:function(){ return "_id : "+this._id+" ; paramname : "+this.paramname; } }); <template name="parent"> {{#each nodes}} {{> child myparam}} {{/each}} </template> <template name="child"> {{! going output same stuff}} <div>_id : {{_id}} ; paramname : {{paramname}}</div> <div>{{someotherhelper}}</div> </template>
depending on you're precisely trying achieve, there might better approach 1 gets job done @ least.
Comments
Post a Comment