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

Path accessor to prevent walking up the scope #645

Closed
daffl opened this issue Jan 6, 2014 · 6 comments
Closed

Path accessor to prevent walking up the scope #645

daffl opened this issue Jan 6, 2014 · 6 comments
Milestone

Comments

@daffl
Copy link
Contributor

daffl commented Jan 6, 2014

There are some valid use cases where walking up the scope for a property lookup is not what you want. For example:

{
  title: 'CanJS school',
  children: [{
    name: 'first'
  }, {
    name: 'second',
    title: 'Second title'
  }]
}

And

{{#each children}}
  <p>{{title}}</p>
{{/each}}

Will render CanJS school as the first item. The best solution is probably prefixing the property name with a ./ accessor if you don't want it to be looked up in the parent scope:

{{#each children}}
  <p>{{./title}}</p>
{{/each}}
@stevenvachon
Copy link
Contributor

Is {{#each children}} supposed to simulate {{#children}} to begin with? I'd have thought that a {{#with children}} would be necessary.

@matthewp
Copy link
Contributor

matthewp commented Jan 6, 2014

@stevenvachon You can use either #each children or #children for arrays.

As for the issue, personally I would rather fix this issue in JavaScript rather than adding the complexity of paths to mustache.

@savinger
Copy link

savinger commented Jan 6, 2014

@matthewp Is there some straightforward way to fix this in javascript beyond renaming attributes? I have handlebars templates that I'm trying to transition to mustache right now that render something inherently recursive. So this abstract object can have one or more similar objects within it, so they all share the same attributes, but each does not necessarily have all. All that to say renaming isn't really an option.

Just hoping that may motivate the issue a bit.

@stevenvachon
Copy link
Contributor

@matthewp

Yes, but I was referring to the context change. #each and #key do behave the same in this regard, though, despite my guessing otherwise.

Also, I'm not so sure that it can be fixed in JavaScript. #each is behaving correctly by referencing its parent scope when it cannot find a variable.

@matthewp
Copy link
Contributor

matthewp commented Jan 6, 2014

Talked this over off-issue, I wasn't aware that ../ already exists so I withdraw my objection. @savinger

@daffl
Copy link
Contributor Author

daffl commented Apr 7, 2014

Fixed via #874

@daffl daffl closed this as completed Apr 7, 2014
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