Skip to content Skip to sidebar Skip to footer

Switch Case In Mustachejs

Using Mustache js (a logic less templating) is there a way i can achieve switch case? this i need because a class is assigned to dom element based on the value for example: switch(

Solution 1:

There's a few ways to do this.

In Javascript, if Mustache encounters a function in a value, it will call it using the enclosed text as the only argument.

var data = {
    foo: function(text) { return'<b>' + text + '</b>'; }
}

mustache

{{#foo}}
   HI I LIKE FISH, thanks.
{{/foo}}

outputs

<b>HI I LIKE FISH, thanks.</b>

Search for "lambda" in the mustache docs.

Another way to do it is do a falsey/truthy check.

data

{ foo:true }

mustache

{{#foo}}
  output this iftrue.
{{/foo}}
{{^foo}}
  output iffalse
{{/foo}}

Post a Comment for "Switch Case In Mustachejs"