Khalid Dabjan
Khalid Dabjan

Reputation: 2797

check for boolean false in mustache.js

According to this question, checking for false in a json object that you pass to mustache template is done like this:

{{^like}}
    it is false
{{/like}}
{{#like}}
    it is true
{{/like}}

assuming that the json looks like this {"like":true}

but trying this out on the Mustache demo page is not working out as expected. The html output is shown like this:

it is true
it is false
{{/like}}

Why is it breaking when you have {{^whatever}} in your template? Is it not the right way to check for false?

Upvotes: 2

Views: 3322

Answers (1)

Shalom Aleichem
Shalom Aleichem

Reputation: 3027

Inverted sections (^) are implemented on the Mustache github, but the MustacheDemoIO uses obsolete version of the library that doesn't support that. In the obsolete code you can find:

// for each {{#foo}}{{/foo}} section do...
  return template.replace(regex, function(match, name, content) {
    var value = that.find(name, context);
    if(that.is_array(value)) { // Enumerable, Let's loop!
      return that.map(value, function(row) {
        return that.render(content, that.merge(context,
                that.create_context(row)), partials, true);
      }).join("");
    } else if(value) { // boolean section
      return that.render(content, context, partials, true);
    } else {
      return "";
    }
  });

but there is nothing for {{^foo}} {{/foo}}. That's why it brakes on your example.

But that shouldn't be a big problem, because the newest version that is available on the Mustache github has this functionality.

Upvotes: 1

Related Questions