Using conditionnal formating with implicit iterator #202
Replies: 1 comment
-
|
Thank you for reaching out @TommyTEnviroQc, and welcome to the discussions. I moved your question here, because we reserve the issue tracker for discussing concrete issues regarding the test files. There is currently no way to compare context values to literal values, especially not right from the template. I will discuss a few other ways in which you could still get the result you want. Option 1You are essentially taking a value from the context and then formatting it in a way that is different from what the host programming language would do by default. This is one of the main use cases for which filter syntax (#153) has been proposed. Filters are a nonstandard extension to the language (at least for now), but there are multiple Mustache engines that implement them anyway and their implementations tend to be somewhat compatible, so you might still be able to use them. It starts with defining a simple function in the host language that takes the context value and returns the formatted value. Here's what that might look like in JavaScript: // Permanent helper object
var numberNames = {
'1': 'one',
'2': 'two',
'3': 'three',
...
};
// The formatting function
function number2word(number) {
return numberNames[number];
}Next, in the data that you pass to the template, you include the formatting function along with the data to format. {
"numbers": [ "1", "2", "3", "4" ...],
numAsWord: number2word,
}Finally, in the template, you pass context values to formatting functions by writing them with a pipe in between. You can use this both in interpolation tags and section tags. {{#numbers}}
{{ . | numAsWord }}
{{/numbers}}Option 2Some Mustache engines provide more exotic extensions to the Mustache language, which might let you achieve the same effect entirely from the template alone. It highly depends on the engine whether there is such a feature available and what the syntax will look like. If you use such syntax, your template will not be portable to other engines. I cannot recommend this route, but I mention it for completeness because it does exist. Option 3Any time the Mustache language does not provide built-in functionality for what you want to do, the catch-all solution is to preprocess the data so that it already contains what you want to render. While this can be inconvenient, there is also an advantage to preprocessing: by moving some responsibilities to the host language, the template language can stay lean. It is an explicit goal of Mustache to be minimalistic. The {
"numbers": [ "1", "2", "3", "4" ...].map(number2word)
}{{#numbers}}
{{ . }}
{{/numbers}}You can try this out in the playground by pasting the following code into the load/store box: If you want to retain the option to render the original, non-formatted number as well, simply replace each item in the array by an object that has both versions of the value: function number2object(number) {
return {
number: number,
text: numberNames[number],
};
}
{
numbers: [1, 2, 3, 4, 5].map(number2object)
}{{#numbers}}
{{ number }}: {{ text }}
{{/numbers}}Playground savestate: I hope this helps! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi!
I'm trying to display special charactors and space for rendering of a simple (top level) list, something like
"numbers": [ "1", "2", "3", "4" ...]And in my rendering, I'm trying to assign / format the data so that if a certain number is present (the list is dynamic), it will display as "one", "two" ... instead of the current value.
I've tried numerous things, but I can't seem to find how to compare the current value to the desired output.
Is there a way to do something along the lines of:
{{#ifCond . '==' '1'}} blegh {{/ifCond}}Thank you!!
Beta Was this translation helpful? Give feedback.
All reactions