Closed karthyk16 closed 10 months ago
@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 https://github.com/janl/mustache.js/issues/399 in the JavaScript implementation for more context. Thank you.
Isn't that already covered by the context stack?
A {{name}} tag in a basic template will try to find the name key in the current context. If there is no name key, the parent contexts will be checked recursively.
It does rely on unique names though. You may also be able to explicitly reference the name of the parent, which will bring it back to the top of the context stack.
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: https://jsfiddle.net/vwf3u4bg/
Ahh, in your example, it's not in the context stack.
{{#shrek.ogres.are}}
Deep in scope. Ogres are: {{super}} </br>
Escape scope? Ogres have: {{ogres.have}} </br>
Access from root? Shrek Ogres have: {{shrek.ogres.have}} </br>
{{/shrek.ogres.are}}
The stack at the point is only:
shrek.ogres.are
[root]
{{ogres.have}}
will first look for shrek.ogres.are.ogres.have
, then ogres.have
If you add shrek
(or parent of the parent) to the stack, it works:
{{#shrek}}
{{#ogres.are}}
Deep in scope. Ogres are: {{super}} </br>
Escape scope? Ogres have: {{ogres.have}} </br>
Access from root? Shrek Ogres have: {{shrek.ogres.have}} </br>
{{/ogres.are}}
{{/shrek}}
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)
}
}
}
Deep in scope. Ogres are: Cool Escape scope? Ogres have: Layers Access from root? Shrek Ogres have: Layers
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: /z/../../../a
, or arbitrarily access any layer.
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 https://github.com/mustache/spec/discussions/163, https://github.com/mustache/spec/discussions/153 and https://github.com/mustache/spec/discussions/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.