Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
msvticket opened this issue Jul 8, 2014 · 1 comment
Closed

Comments

@msvticket
Copy link

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

@msvticket
Copy link
Author

I added a separate pull request instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant