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

../myVar should reference the rootContext #48

Closed
mschipperheyn opened this issue Aug 3, 2012 · 5 comments
Closed

../myVar should reference the rootContext #48

mschipperheyn opened this issue Aug 3, 2012 · 5 comments
Assignees
Milestone

Comments

@mschipperheyn
Copy link

The .combine(someContext) allows me access to some global scoped variables. However, in order to maintain compatibility with my Javascript version, I need to be able use Handlebars Paths to be able to reference the root scope.

The Handlebars documentation references Paths as a way to reference the root scope (it's not entirely clear if it should be parent or root scope, but root scope would make the most sense).

Right now using {{../myVar}} throws an error. It should either display the variable or nothing from within a partial.

jknack added a commit that referenced this issue Aug 4, 2012
* ../myVar should reference the rootContext #48
* ../ point to the parent context
@jknack
Copy link
Owner

jknack commented Aug 4, 2012

Just one comment ../ is reference to a parent scope which might or might not the root scope. This test demostrate how it works.

Thanks, Marc.

@ghost ghost assigned jknack Aug 4, 2012
@jknack jknack closed this as completed Aug 4, 2012
@mschipperheyn
Copy link
Author

Are you sure about that? From the discussions I saw, ../ refers to the root template.
To me, referring to the parent wouldn't make sense at all, because you can never know at what level a partial will be included.

@jknack
Copy link
Owner

jknack commented Aug 5, 2012

Yes, I'm sure.

{
 "foo": {
   "bar": {
     "title": "bar"
   },
   "title": "foo"
 },
 "title": "root"
}

with:

{{#foo}}
  {{#bar}}
   {{title}}
  {{/bar}}
{{/foo}}

The output is: bar

with:

{{#foo}}
  {{#bar}}
   {{../title}}
  {{/bar}}
{{/foo}}

output is: foo

with:

{{#foo}}
  {{#bar}}
   {{../../title}}
  {{/bar}}
{{/foo}}

output is: root

with:

{{#foo}}
  {{#bar}}
   {{../../../title}}
  {{/bar}}
{{/foo}}

output is: ""

You can try all these here: http://tryhandlebarsjs.com/

@mschipperheyn
Copy link
Author

Hmm, the interesting question then also becomes: how to reference a global variable (such as serverUrl) when you can't predict at which level a partial is included

@jknack
Copy link
Owner

jknack commented Aug 5, 2012

I found something weird. When I started with handlebars I was under the assumption that Handlebars.js was a Mustache complaint implementation.

Now, in Mustache if a value isn't found in the current scope, that value is resolved against the context stack, i.e., it looks in the parent until the value if found or until the root context is reach.

So, you can safely refer to {{serverUrl}} in handlebars.java bc it is a Mustache complaint implementation.

The following example is part of the Mustache Spec. It works in handlebars.java but not in handlebars.js

{a: {one: 1}, b: {two: 2}, c: {three: 3}, d: {four: 4}, e: {five: 5}}

Template:

{{#a}}
{{one}}
{{#b}}
{{one}}{{two}}{{one}}
{{#c}}
{{one}}{{two}}{{three}}{{two}}{{one}}
{{#d}}
{{one}}{{two}}{{three}}{{four}}{{three}}{{two}}{{one}}
{{#e}}
{{one}}{{two}}{{three}}{{four}}{{five}}{{four}}{{three}}{{two}}{{one}}
{{/e}}
{{one}}{{two}}{{three}}{{four}}{{three}}{{two}}{{one}}
{{/d}}
{{one}}{{two}}{{three}}{{two}}{{one}}
{{/c}}
{{one}}{{two}}{{one}}
{{/b}}
{{one}}
{{/a}}

Output:

1
121
12321
1234321
123454321
1234321
12321
121
1

@jknack jknack mentioned this issue Mar 20, 2013
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