javascript - Accessing parent helper in Meteor -
i find myself dividing work templates still use same helpers.
so, have template structure:
<template name="maintemplate"> <div>{{> firsttemplate}}</div> <div>{{> secondtemplate}}</div> <div>{{> thirdtemplate}}</div> <div>{{> fourthtemplate}}</div> </template>
now each of these templates wants use same helper, let's call datahelper
:
template.maintemplate.helpers({ datahelper: function() { //do stuff return result } })
sadly, helper can't accessed in template first through fourth typing {{datahelper}}
how events work.
my solution has been create global helper instead, seems tad overkill, since have few pages don't care these helpers @ all. solution create 4 separate helpers but, hey, dry.
am missing simple here?
there isn't obvious way in current version of meteor. 1 solution child template "inherit" helpers parent. can pretty using meteor-template-extension. here's example:
html
<body> {{> parent}} </body> <template name="parent"> <h1>parent</h1> {{> child}} </template> <template name="child"> <h2>child</h2> <p>{{saysomething}}</p> </template>
js
template.parent.helpers({ saysomething: function() { return random.choice(['hello', 'dude!', 'i know right?']); } }); template.child.inheritshelpersfrom('parent');
the template child
inherits of parent's helpers has direct access saysomething
.
this technique has 2 drawbacks:
- you have specify
inheritshelpersfrom
relationship - all of parent's helpers inherited
Comments
Post a Comment