7

I have a scenario where I am getting {{isdone}} value with Boolean data.

I want to be printed as "pending" for false value and "Done" for true.

I'm using below code, Which isn't working.

{{isdone}} == false ? "pending" : "Done"
Seblor
  • 6,947
  • 1
  • 25
  • 46
Deepesh kumar Gupta
  • 884
  • 2
  • 11
  • 29
  • are you doing it in anuglar or something else? – mehta-rohan Jul 03 '18 at 09:01
  • 2
    Mustache templates are said to be "logic-less", though you can have `if/else` statements in them (See this question for more info : https://stackoverflow.com/questions/6027525/how-do-i-accomplish-an-if-else-in-mustache-js). I highly doubt a ternary operator is possible in a template. – Seblor Jul 03 '18 at 09:04
  • Thanks @Seblor , Mean while I googled and found the same, – Deepesh kumar Gupta Jul 03 '18 at 09:24

3 Answers3

2

Use the ^ block for else.

You can (now) use the ^ block for an else or false condition. Something like this should work:

{{#isdone}}Done{{/isdone}}{{^isdone}}pending{{/isdone}}

Or as a more readable multi-line block of code:

{{#isdone}}
Done
{{/isdone}}
{{^isdone}}
pending
{{/isdone}}
Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
1

As long as you have control of your context data, correct way is to pass another variable, that will already contain pending or Done beforehand.

If you don't have control over the data, then maybe moustache isn't good for you as you may need template engine that can have some more logic in it to transform data a bit.

K.H.
  • 1,383
  • 13
  • 33
0

You might want to register a ternary helper for that

Handlebars.registerHelper("ternary", function (condition, trueValue, falseValue, options) {
   return condition ? trueValue : falseValue;
});

and then in your templates use it like

{{ternary isdone "Done" "pending"}}
Willem Mulder
  • 12,974
  • 3
  • 37
  • 62