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 #822

Closed
wants to merge 1 commit into from

Conversation

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

… regard to what is regarded as walse for blocks.
@kpdecker
Copy link
Collaborator

kpdecker commented Jul 9, 2014

This is a large change in behavior from the existing that is going to make someone unhappy. Unfortunately we need to side with the existing implementation here.

Users can override blockHelper missing if they would like this functionality and we might be willing to entertain a compat flag to support this in the native, off by default.

@kpdecker kpdecker closed this Jul 9, 2014
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

Successfully merging this pull request may close these issues.

2 participants