Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieving global options from helpers.js #6

Open
Snugug opened this issue Jun 14, 2013 · 1 comment
Open

Retrieving global options from helpers.js #6

Snugug opened this issue Jun 14, 2013 · 1 comment

Comments

@Snugug
Copy link

Snugug commented Jun 14, 2013

How can I retrieve the global options from within helpers.js? Say, for instance, I've got the following:

'image-url': function(src) {
  return this.options.assets + '/' + this.options.userConfig.assets.imagesDir + '/' + src;
}

That will automatically prefix image urls. It works fine, except if I'm using from within a loop, in which case this is the src and not the global options. How can I point this to the global options?

@clavery
Copy link
Owner

clavery commented Jun 17, 2013

Unfortunately this is how handlebars handles context. this is always set to the current context which in a block level helper (like a loop) will not be set to the global context. There is current an open issue referencing this: handlebars-lang/handlebars.js#392

One workaround would be to reference the parent context using the ../ syntax. So in your template you might have

{{#each images}}
    {{../options.imagesDir}}{{src}}
{{/each}}

Unfortunately you probably can't do this from within a helper. You also need to specify the right number of ../ depending on how deep your context is. So if you are two loops deep you would need ../../ to reach the root context. There may be a more elegant workaround I'm not thinking of.

I'm considering making the renderer used configurable and adding support for https://github.com/linkedin/dustjs because I think that would be more powerful than handlebars. I think adding support to override handlebars from the grunt config with a custom render function first would be best.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants