Skip to content

Implement false for blocks like mustache(.js) and handlebars.java #821

@msvticket

Description

@msvticket

Essentially I think the following tests should succeed:

it("inverted section with empty string", function() {
  var string = "{{#goodbyes}}{{this}}{{/goodbyes}}{{^goodbyes}}Right On!{{/goodbyes}}";
  var hash = {goodbyes: ""};
  shouldCompileTo(string, hash, "Right On!", "Inverted section rendered when value is empty string.");
});

it("inverted section with value NaN", function() {
  var string = "{{#goodbyes}}{{this}}{{/goodbyes}}{{^goodbyes}}Right On!{{/goodbyes}}";
  var hash = {goodbyes: NaN};
  shouldCompileTo(string, hash, "Right On!", "Inverted section rendered when value is NaN.");
});

it("inverted section with value 0", function() {
  var string = "{{#goodbyes}}{{this}}{{/goodbyes}}{{^goodbyes}}Right On!{{/goodbyes}}";
  var hash = {goodbyes: 0};
  shouldCompileTo(string, hash, "Right On!", "Inverted section rendered when value is 0.");
});

Rationale

mustache(5) states

"False Values or Empty Lists

If the person key exists and has a value of false or an empty list, the HTML between the pound and slash will not be displayed."

In javascript 0, NaN and the empty string are false. This is explicitly stated for the Javascript implementation of mustache: https://github.com/janl/mustache.js#false-values-or-empty-lists

(false, undef and empty lists are already interpreted as false in handlebars.js.)

There seem to be a slight disagreement within the moustache community whether the javascript notion of false should be used in all implementation or if you should use the notion for each specific language, but that the implementations in javascript should use the javascript notion of false is clear.
(See discussion in mustache/spec#28 )

It can also be noted that the java implementations of handlebars tries to follow the javascript behavior. The method isEmpty is used to decide if a value is false: https://github.com/jknack/handlebars.java/blob/master/handlebars/src/main/java/com/github/jknack/handlebars/Handlebars.java#L176

So at the moment handlebars.java follows the behaviour of moustache.js but handlebars.js does not. (Something that just bit me...)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions