Comments (6)
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.
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.
@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.
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.
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.
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)
- Make current index available for lists HOT 2
- How to escape a partial's output? HOT 2
- Mustache-ES6 HOT 1
- time complexity of mustache HOT 1
- Help with sections HOT 3
- use value from list based on another key HOT 1
- Appending random string starting with 'm_-' to classes after rendering the template HOT 1
- C#/ Net Standard implementation HOT 1
- Mustache substitution minimum string length HOT 1
- Not able to generate template for the Map<String, Map<String, List<Map<String, String>>>>
- Context name from list HOT 9
- Pass variables to different template/entries HOT 1
- Problem with html-loader and mustache HOT 2
- Mustache need a standard helper-library? HOT 3
- {{#item.0}} checks HOT 1
- Add line numbers. HOT 2
- How to output raw moustache in a code block? HOT 2
- This repository is about the website only (click here to see where to take your comments)
- How to add dynamic style HOT 1
- Elm implementation HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mustache.github.com.