In a Mustache template, how can I show a block containing a value only when said value is a non-false value?
I need to be able to show or not show an entire block, not just the value.
For example, in Handlebars:
{{#if name}}
Hi {{name}}!
{{/if}}
But in Mustache, "when the value is non-false but not a list, it will be used as the context (emphasis mine) for a single rendering of the block.":
{{#person?}}
Hi {{name}}!
{{/person?}}
But I don't want to set context, I want to check the key and have direct access to it's value inside the block.
I made the following unsuccessful attempts:
{{#name}}
Hi {{name}}!
{{/name}}
{{#name}}
Hi {{this}}!
{{/name}}
{{#name}}Hi {{name}}!{{/name}}
should just work fine, since a string has to prevent the section rendering if it is missing or empty. The fact that the string enters the context stack should not bother you: the inner {{name}}
tag will still fetch and render the string.
If {{#name}}Hi {{name}}!{{/name}}
does not behave as expected, there are two possible explanations: your input data is not the one you think it is, or you are using a Mustache implementation that does not follow the specification (and it is an issue).