Comments (9)
Sure. Check the template delegate guide, linked from the README. It covers exactly this use case.
from spec.
Maybe I'm missing something but I wasn't able to find it. Are you referring to the mustache spec README.md?
from spec.
Nope. Sorry I'm on a mobile phone and copy pasting a link is not so easy : look for "tag delegate" in the GRMustache home page on github. Follow the link. There is the solution to your issue.
Tell me if it works as I expect.
from spec.
Oops, sorry, wrong repo. My mistake. Mustache has no such feature. Some implementations do, such as GRMustache. I advise you to check with your current Mustache implementation.
from spec.
Unfortunately, the templates I write need to work under multiple implementations, so I can't rely on implementation-specific code.
from spec.
Yes. But you can open issues in each of these implementation repositories.
Before making the exact same suggestion, please open your eyes, and see that your suggestion:
- implies parsing of literal values
- implies, for consistency's sake, that
{{ "Hello" }}
would render "Hello" - arbitrarily, for no good reason whatsoever, limits default values to parsable literals (string, numbers, maybe boolean, but certainly not full objects or dates for example)
Some implementations provide with the feature you are looking for in a much more general way. Please, please, please, don't suggest JS/PHP implementations to implement your ||
syntax.
from spec.
I understand your concerns. If mustache doesn't currently parse literal values this would indeed be a much larger undertaking than simply adding a new operator. The ||
syntax I suggested may not be the best way to implement this. I'm certainly open to doing it in other ways, perhaps as a callback function that could generate a default value. Or maybe if the spec had a filter syntax as is being discussed in #41 no changes would be necessary as you could use a filter to do it ({{foo | defaultToNone}}
).
from spec.
Yes @spudly, I agree filters could do that as well.
In GRMustache, you have two ways to provide default values:
- use the "tag delegate" pattern I was talking above. This is a mechanism that invokes a callback each time a value will get rendered. The callback has the opportunity to confirm or alter the value - including returning something instead of null.
- define and use filters:
{{ defaultToNone(foo) }}
. GRMustache filters are functions that can return any value (not only text like pipe-filters), and can also fill sections, so that you can for instance render array indexes{{# withPosition(items) }} {{ position }}: {{ name }} {{ / }}
.
This issue is typical of an use case that does not deserve a specific solution. It needs (#41), or has (GRMustache delegate and filters) more general mechanisms that include a solution for it.
from spec.
Closing in favor of #41.
from spec.
Related Issues (20)
- Newline disappears after # tags, tab dissappears before # tags HOT 2
- Possible Improvement to Comments #8 HOT 1
- Could this repository be maintained again? HOT 17
- Variable test in sections.yml doesn't conform to internal whitespace expectations HOT 7
- incorrect json test data
- Why "Lambdas used for inverted sections should be considered truthy"? HOT 12
- Proposal on how to specify indentation of blocks HOT 3
- how to format date in mustache syntax with json template? HOT 4
- Specification is ambiguous on dotted name context handling HOT 3
- Idea: power lambdas HOT 80
- Comments: Behavior on variable name collision HOT 1
- {{#item.0}} checks and context HOT 7
- [inheritance.yml] Override parent with newline spec. Are block tags standalone? HOT 10
- Are Lambda sections standalone? HOT 2
- Please close/delete the issues
- Proposal: Trim whitespace/newlines to get improved readability? HOT 5
- How should empty tags be treated? HOT 2
- Provide clearer documentation as to the logic-less advantage of Dynamic Names? HOT 4
- The specification does not specify how to handle tags with periods in them HOT 5
- HelperIn this way, the test seems like you are purely testing for the absence of functionality. I think it is more motivating if you contrast it with the desired behavior: HOT 1
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 spec.