4

How do we write the following in mustache?

I want to check if a variable exists, if it does, I want to check if that variable equals another variable. so

This would be the PHP equivalent

if ($example && $example == $type) {
}
J K
  • 4,883
  • 6
  • 15
  • 24

3 Answers3

2

Alternatively mustache shortcodes use the # symbol for 'if' so assuming your shortcode is {{example}} then

{{#example}} 
    Start condition if example exists
{{/example}}

And ^ works the same way for 'if does not exist'

{{ ^example}}
    Start condition if example does not exist
{{/example}}

NOTE - If using mustache conditions you must must remember to close the condition the same way you would an HTML attribute.

Scott
  • 36
  • 2
1

If you're using mustache the goal is to perform this type of logic in the controller, and not in the template. As per How do I accomplish an if/else in mustache.js? You would want to do this type of check in the controller and generate a boolean that you can use the in the template

Community
  • 1
  • 1
Coding Orange
  • 548
  • 4
  • 7
1

You can attach a closure to the mustache data and put this logic into that closure, something like:

var md = {};
md['my_condition'] = function(){
  return (this['some_attr'] && this['some_attr'] == 'expected_val');
}

and then use the md object as data to the mustache template.

MohamedSanaulla
  • 6,112
  • 5
  • 28
  • 45