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

Fix for inverted sections where value is an empty string or zero. #188

Closed
wants to merge 1 commit into from

Conversation

appmode
Copy link

@appmode appmode commented Mar 16, 2012

This fixes Issue #186
A numeric 0 or an empty string will be matched by an inverted section as they equate to false.

I have also added support for use of an empty inverted section tag {{^}} as an else style tag for sections,
usage:
{{#section}}
section
{{^}}
inverted section
{{/section}}

This is equivalent of:
{{#section}}
section
{{/section}}
{{^section}}
inverted section
{{/section}}

adds support for an else style tag {{^}} for sections,
usage:
{{#section}}
    section
{{^}}
    inverted section
{{/section}}
@gonchuki
Copy link

the Mustache spec does not specify an "else" tag. You should consider removing that change from this pull request.

@appmode
Copy link
Author

appmode commented Mar 29, 2012

There's no new functionality added here and no new tag either, all I've done is add a shorter and more readable way of doing it. I think it's a sensible and I'd like to see it merged.

@gonchuki
Copy link

You need to understand that the idea of Mustache templates is that they are interoperable among any implementation on any programming language.
If you have a template with this "else" tag, it will not render as you expect in any of the existing nor future Mustache implementations until this gets added to the spec, and for this to happen you need to first ask for it in mustache/spec.

To any extent, you are breaking a basic collaboration rule: one change per pull request. Having those 2 changes squashed in a single commit makes it impossible to back one of them out or cherry pick this change into another repo, which even being non-spec conformant some devs would still want to have it on their own Mustache.js forks.

@hswolff
Copy link

hswolff commented Jun 20, 2012

Just ran into this issue today.

If the value of a key is 0 then no section is rendered.

Example:

var view = {
    hello: 0,
    hello2: '0',
    hello3: ''
};

var template = "{{hello}}";
Mustache.render(template, view);
// "0"

var template2 = "{{#hello}}full{{/hello}}{{^hello}}empty{{/hello}}";
Mustache.render(template2, view);
// ""

var template3 = "{{#hello2}}full{{/hello2}}{{^hello2}}empty{{/hello2}}";
Mustache.render(template3, view);
// "full"

var template4 = "{{#hello2}}full{{/hello2}}{{^hello2}}empty{{/hello2}}";
Mustache.render(template4, view);
// "full"

Template 2 should render "empty" but it does not.

I tried this patch and it fixes this issue.

@hswolff
Copy link

hswolff commented Jun 20, 2012

Also I agree w/ @gonchuki the feature {{^}} in this PR should be removed and made into its own PR so specific discussion can take place.

However I do hope the other fix does make it into trunk.

@mjackson
Copy link
Contributor

This issue should be resolve in this commit.

@mjackson mjackson closed this Aug 31, 2012
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.

4 participants