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

Global context not available in a loop? #183

Closed
sontek opened this issue Feb 19, 2012 · 3 comments
Closed

Global context not available in a loop? #183

sontek opened this issue Feb 19, 2012 · 3 comments

Comments

@sontek
Copy link

sontek commented Feb 19, 2012

If I have the following context:

{{model: {username: 'sontek'}, timezones: ['a', 'b', 'c']} 

and I do:

{{#each timezones }}
     {{model.username}}
{{/each}}

model doesn't exist so I can't access the username.

@sontek
Copy link
Author

sontek commented Feb 19, 2012

I found I can use ../model.username but can't find docs on that, I just found it in some bug reports.... Is that documented somewhere?

@spadgos
Copy link
Contributor

spadgos commented Feb 28, 2012

It's in the docs under "Paths"

Nested handlebars paths can also include ../ segments, which evaluate their paths against a parent context.

<h1>Comments</h1>

<div id="comments">
  {{#each comments}}
  <h2><a href="/posts/{{../permalink}}#{{id}}">{{title}}</a></h2>
  <div>{{body}}</div>
  {{/each}}
</div>

Even though the link is printed while in the context of a comment, it can still go back to the main context (the post) to retrieve its permalink.
The ../ path segment references the parent template scope, not one level up in the context. This is because block helpers can invoke a block with any context, so the notion of "one level up" isn't particularly meaningful except as a reference to the parent template scope.

@wagenet wagenet closed this as completed May 4, 2012
@iancrowther
Copy link

Many thanks @sontek and @spadgos

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

4 participants