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

inverted sections where value is an empty string #186

Closed
appmode opened this issue Mar 15, 2012 · 10 comments
Closed

inverted sections where value is an empty string #186

appmode opened this issue Mar 15, 2012 · 10 comments

Comments

@appmode
Copy link

appmode commented Mar 15, 2012

If value is an empty string it is not matched by either a section or an inverted section.

value = ""

{{#value}}
value is {{value}}
{{/value}}
{{^value}}
value is empty
{{/value}}

will output nothing.

The man page says:
A section will be rendered if it "has a non-false value"
An inverted section "will be rendered if the key doesn't exist, is false, or is an empty list."

An empty string equates to false and therefore the expected behaviour would be for an empty string to be matched by an inverted section.

@fofr
Copy link

fofr commented Apr 11, 2012

I've noticed a discrepancy regarding this issue between this JS implementation and the PHP Mustache version developed here: https://github.com/bobthecow/mustache.php

The PHP version treats an empty string as falsy. In the case above it would output: value is empty.
This version, as outlined above, doesn't match either.

@bobthecow
Copy link

Per the spec, empty strings should be falsey if the host language treats them as falsey.

@bobthecow
Copy link

@xeoncross
Copy link

I noticed this also. Empty strings don't seem to work - I had to convert all empty strings to null to get inverted sections working. Also the value of false seems to give odd results sometimes.

@Sarkie
Copy link

Sarkie commented Apr 25, 2012

Came here to report the similar issue so from this page

http://mustache.github.com/mustache.5.html

In the Inverted Sections there's a template

Putting it into the demo

http://mustache.github.com/#demo

Doesn't produce the correct outcome and that's using mustache.js, is this the same issue as this or a new one?

bgreenlee added a commit to bgreenlee/mustache.js that referenced this issue May 1, 2012
…o false) as falsey in inverted sections.

Fixes issue janl#186 (janl#186)
@jnewman
Copy link

jnewman commented May 11, 2012

+1 This is pretty critcal.

@zernyu
Copy link

zernyu commented May 11, 2012

+1

@jakearchibald
Copy link

Just ran into this too. Agree with bobthecow, the spec states that falsey values should be treated as empty lists. This is what pystache does.

@mjackson
Copy link
Contributor

I'd prefer not to make strings falsey in mustache.js to preserve compatibility with people's working code. However, if this is the consensus of the community then we can definitely go in that direction. By that same reasoning 0 would also become falsey, as well as NaN. Is anybody opposed to this change?

@bgreenlee
Copy link

I am not opposed to this change. 0, NaN, and "" all evaluate to false when coerced (with !!), so they should trigger an inverted section according to the spec:

If the data is not of a list type, it is coerced into a list as follows: if
the data is truthy (e.g. !!data == true), use a single-element list
containing the data, otherwise use an empty list.

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

10 participants