Search code examples
meteormeteor-helper

Access an original TemplateInstace from the helper in Meteor


Could anybody point me how to access an original TemplateInstance from the meteor helper. I'm aware of the Template.instance() but it appears to return the template instance where the helper was called, not the template instance for which the helper was defined.

Imagine we have two tiny templates:

<template name='demo'>
  <h1>{{helper}}</h1>
  {{# border}}
    <h2>{{helper}}</h2>
  {{/border}}
</template>

<template name='border'>
  <div style="border:1px solid red">
    {{> UI.contentBlock}}
  </div>
</template>

With the following behavior:

Template.demo.created = function() {
  this.result = "OK";
}

Template.demo.helpers({
  helper: function() {
    var tmpl = Template.instance();
    return tmpl.result || "FAILED";
  }
});

I've expected to obtain two "OK" for the demo template: the second one should be in the red border. But since Template.instance() returns original TemplateInstance only when helper is called at the top level of its owner template the result is "FAILED" (of course in the red border).

Question: Is there any public api to get the original TemplateInstance (without need to traverse view/parentView/_templateInstace)?


Solution

  • It seems that it's known and already fixed (?) Meteor bug. More here: https://github.com/meteor/meteor/issues/3745

    Comment from rclai on GitHub:

    This was already addressed and fixed for the next release.

    Run meteor like this, not sure if it still works: meteor --release [email protected]

    Another alternative is to use aldeed:template-extensions, which has super nice features, especially with dealing with template instances and I believe their way of fetching the template instance is a workaround this issue.