[ 6 posts ]

Marc

YUI Contributor

  • Offline
  • Profile

Handlebars and global variables

Post Posted: Wed Aug 01, 2012 12:34 pm
+0-
Some of the variables I use in my Handlebars templates are global. They tend to be ones I store in YUI_config / Y.config
What I do is that I set this object as part of the model passed to the Handlebars template. Not very elegant.

Code:
render:function(){
            var container = this.get('container'),
            data = {};
            data.YUI_config = Y.config;
            data.messages = this.get("model").toJSON();
            
            container.setHTML(this.template(data));
             
             return this;
         }


I have a few questions about this:

1. Whenever I am in the context of a helper, I lose access to the model elements passed to the template. The helper only "knows" the variables passed to it through the helper interface. This does not happen in my Java implementation of Handlebars and it doesn't make sense to me either. I'm not sure which is right and what is the suggested way for dealing with global variables.

2. Is there perhaps a way to global variables at a central level so that they will always be included as part of the data-model?

Kind regards,
Marc

Marc

YUI Contributor

  • Offline
  • Profile

Re: Handlebars and global variables

Post Posted: Wed Aug 01, 2012 1:12 pm
+0-
Seems to be a known issue. https://github.com/wycats/handlebars.js ... nt-4206666

Marc

YUI Contributor

  • Offline
  • Profile
Tags:

Re: Handlebars and global variables

Post Posted: Thu Aug 02, 2012 7:47 am
+0-
So, according to the Handlebars.js documentation, you should be able to access the parent scope (which seems to suggest the root template scope) from withing a Partial or Helper by using {{../myVar}}.

When I try this I get an error because depth1 is undefined. So, it seems that the ../myVar is parsed correctly to look up a scope one level up. But that level is empty.

Any suggestions here?

Kind regards,
Marc

Eric Ferraiuolo

YUI Developer

  • Username: ericf
  • Joined: Mon Jan 12, 2009 8:26 pm
  • Posts: 380
  • Location: Boston, MA
  • Twitter: ericf
  • GitHub: ericf
  • Gists: ericf
  • IRC: eric_f
  • YUI Developer
  • Offline
  • Profile
Tags:

Re: Handlebars and global variables

Post Posted: Fri Aug 03, 2012 5:47 am
+0-
Are you saying that your Handlebars helper functions are not being called with full context which was passed to the original template function? And this situation is arising when a helper is invoked inside of a partial?

Marc

YUI Contributor

  • Offline
  • Profile
Tags:

Re: Handlebars and global variables

Post Posted: Fri Aug 03, 2012 6:55 am
+0-
Yes, I think that is what I'm saying.

I'll give you an example

Code:
Handlebars
messages.hbs

{{#stripeRows messages "message"}}
   {{#mugshot message.from "sma" "sqr"}}
   {{> mugshot}}
   {{/mugshot}}
{{/stripeRows}}

mugshot.hbs

{{../YUI_config.serverUrl}}/img/{{myIgm}}


Code:
Javascript code
var inbox = Y.Base.create('inbox', Y.App,[],{
   initializer: function () {
      Y.Handlebars.registerPartial("mugshot",templates.template('common-mugshot-hbs'));
   }
[...]

var ListView = Y.Base.create(
   'listView',
   Y.View,
   [],{
      render:function(){
            var container = this.get('container'),
            data = {};
            data.YUI_config = YUI_config
            data.messages = this.get("model").toJSON();
            
            container.setHTML(this.template(data));
            
            return this;
         }
      [...]



I don't see the undefined depth1 error anymore. Could be the 3.6.0pr4->3.6.0 upgrade. But yes, I don't have access to the original context when I use the Path ../YUI_config.serverUrl within a registered partial template.

Based on the Handlebars.js Path docs I think you should be able to. If not, than it would be useful to be able to register a mixin object when you register a partial to be able to expose some global variables.

James Bunt

YUI Contributor

  • Username: unkillbob
  • Joined: Wed Aug 01, 2012 6:03 pm
  • Posts: 4
  • GitHub: unkillbob
  • Gists: unkillbob
  • Offline
  • Profile

Re: Handlebars and global variables

Post Posted: Sun Dec 09, 2012 10:47 am
+0-
Encountered this issue myself, have raised a ticket as I'm pretty sure its a bug (and it used to work in YUI 3.5): http://yuilibrary.com/projects/yui3/ticket/2532995
  [ 6 posts ]
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum