5

Update: The bug logged in the selected answer has been fixed by the meteor devs

In my meteor app I have some coffescript making a global helper:

Handlebars.registerHelper "title",->
    Session.get("title")

and a piece of template:

{{#each edited}}
    <li><a class="left-nav" href="{{entryLink this}}">{{this.title}}</a></li>
{{/each}}

this.title is being overridden by the global helper rather than using the this.title context. (I know because if I remove the global helper it works great)

If I add the following:

Handlebars.registerHelper "debug", ->
    console.log this
    console.log this.title

to the template like this:

{{#each edited}}
    <li><a class="left-nav" href="{{entryLink this}}">{{this.title}}{{debug}}</a></li>
{{/each}}

this.title prints to the console correctly, but is not inserted into the template

Any idea why this is happening or how to make the "this.title" be from the local context

Cœur
  • 37,241
  • 25
  • 195
  • 267
funkyeah
  • 3,074
  • 5
  • 28
  • 47
  • Can i just ask why your using `this` in your template and not just `title'? Is title a field in your `edited` items – Warz Jun 07 '13 at 14:28
  • title is a field in my edited items... I have title mapped to a global helper because I use it in various templates... with {{#each}} I want to iterate over that same title property (but for all existing pages instead of just the current page)... if I use {{title}} in the {{#each}} the global helper still overrides and I get the current title printed out multiple times (at least I think it does, I better check!) – funkyeah Jun 07 '13 at 16:29
  • Wouldn't it be easier to just rename the helper? – emgee Jun 08 '13 at 21:55
  • 1
    yes it would, but then what would I have to complain about? – funkyeah Jun 27 '13 at 21:29

2 Answers2

3

Looks like a bug to me, since https://github.com/meteor/meteor/wiki/Handlebars#expressions-with-dots says:

Paths starting with this always refer to properties of the current data context and not to helpers.

I submitted an issue for it: https://github.com/meteor/meteor/issues/1143

Andrew Wilcox
  • 1,021
  • 6
  • 8
1

I read something about this on the handlebarsjs.com website today. Give this a try:

Handlebars also allows for name conflict resolution between helpers and data fields via a this reference:

{{./name}} or {{this/name}} or {{this.name}}

Any of the above would cause the name field on the current context to be used rather than a helper of the same name.
Michael
  • 315
  • 2
  • 8
  • I'll try {{./name}} and {{this/name}} but as you can see from my example {{this.name}} is what I am currently using and it isn't working – funkyeah Jun 03 '13 at 19:09
  • I think the problem may be that handlebars isn't completely implemented or it's a bug. There are handlebar features missing like `@keys` and `@index`. Might get a better answer by posting the question on [meteor-talk](https://groups.google.com/group/meteor-talk‎) – Michael Jun 05 '13 at 16:50
  • I actually did post there before I posted here... still haven't heard anything from anyone there... https://groups.google.com/forum/?fromgroups=#!topic/meteor-talk/0Tj9l3Ql11E – funkyeah Jun 07 '13 at 00:28
  • Ember.js guys made it work. Give it a try with Meteor ? http://stackoverflow.com/questions/12366848/handlebar-helper-inside-each – gabrielhpugliese Jun 07 '13 at 04:03
  • I read that as the opposite problem... In the link the person couldn't get the global helper to work because the template was looking up the local property of the template instead of using the global helper. I want to use the local context property instead of a global helper of the same name. In my example I would think the usage of 'this.property' would make it explicit that the local property should be used instead of the global helper. – funkyeah Jun 07 '13 at 14:10
  • Oh I see. Sorry about that. – gabrielhpugliese Jun 07 '13 at 14:21
  • No worries, I still appreciate the response, and the method you pointed me to does give me some ideas for implementing custom a helper as a workaround for this issue (even if it is a bit hacky) – funkyeah Jun 07 '13 at 16:20