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) {
}
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.
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
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.