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
How to access the parent variable.? #103
Comments
@locks You seem to be one of the last people to actively maintain this. Mustache is a very common templating format all around the internet, do you think you could help us in this discussion? I believe that mustache could use a couple of small additions, access to parent scope being one of these additions. What do you think? Please read the heated discussion in the the linked issue janl/mustache.js#399 |
Isn't that already covered by the context stack?
It does rely on unique names though. |
Hey @DeeHants ! Based on my test here, you can access the root, but you can't access a parent object by name even if it's unique: |
Ahh, in your example, it's not in the context stack.
The stack at the point is only:
If you add
|
Oh yeah, right. Silly me. I guess I just my brain doesn't read templates the same as code. I guess the analogy would be like this: var deepData = {
shrek: {
ogres:{
have: "Layers"
,are: {
süper: "Cool"
}
}
}
}
// `with` is not good practice, but it's fine for this example.
// I imagine something different happens under the hood of mustache.
with(deepData){
with(deepData.shrek){
with(ogres.are){
console.log('Deep in scope. Ogres are: ', süper)
console.log('Escape scope? Ogres have: ', ogres.have)
console.log('Access from root? Shrek Ogres have: ', shrek.ogres.have)
}
}
}
So, you can access any parent as long as you've declared it in the context. Makes perfect sense. You just can't walk backwards like you can with directories: So, in a scenario like the example in #399 they just needed to declare the parent in the scope, but there's no way to reference the root either, so they would need to wrap it at least. Just like this answer where the person says "You need to wrap it all with a parent element you can reference." I suppose it would be nice to have a symbol to represent the root node so you don't have to wrap it. Something variables can't start with. |
Closing this because this repository is about the website and not about the Mustache template language. Please take comments about the template language to https://github.com/mustache/spec/discussions. Previous discussions about this particular topic can be found via mustache/spec#163, mustache/spec#153 and mustache/spec#154. |
I am accessing some data from the JSON. Once i access the child element, i don't have reference to the parent element. How to manage this other than passing the parent element when accessing the child element.
The text was updated successfully, but these errors were encountered: