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

How to access the parent variable.? #103

Closed
karthyk16 opened this issue Nov 3, 2017 · 6 comments
Closed

How to access the parent variable.? #103

karthyk16 opened this issue Nov 3, 2017 · 6 comments

Comments

@karthyk16
Copy link

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.

@adjenks
Copy link

adjenks commented Dec 27, 2018

@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
in the JavaScript implementation for more context.
Thank you.

@DeeHants
Copy link

DeeHants commented Sep 5, 2022

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.

@adjenks
Copy link

adjenks commented Sep 6, 2022

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/

@DeeHants
Copy link

DeeHants commented Sep 6, 2022

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}}

@adjenks
Copy link

adjenks commented Sep 9, 2022

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.

@jgonggrijp
Copy link
Member

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.

@jgonggrijp jgonggrijp closed this as not planned Won't fix, can't repro, duplicate, stale Nov 13, 2023
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