Git Product home page Git Product logo

Comments (6)

DeeHants avatar DeeHants commented on August 16, 2024 1

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

from mustache.github.com.

jgonggrijp avatar jgonggrijp commented on August 16, 2024 1

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.

from mustache.github.com.

adjenks avatar adjenks commented on August 16, 2024

@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.

from mustache.github.com.

DeeHants avatar DeeHants commented on August 16, 2024

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.

from mustache.github.com.

adjenks avatar adjenks commented on August 16, 2024

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/

from mustache.github.com.

adjenks avatar adjenks commented on August 16, 2024

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.

from mustache.github.com.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.