inverted sections where value is an empty string #186

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

Projects

None yet

10 participants

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

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

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 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 bgreenlee added a commit to bgreenlee/mustache.js that referenced this issue May 1, 2012
@bgreenlee bgreenlee Treat an empty string (or for that matter, any value that evaluates t…
…o false) as falsey in inverted sections.

Fixes issue #186 (janl#186)
b257bc6
jnewman commented May 11, 2012

+1 This is pretty critcal.

zernyu commented May 11, 2012

+1

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.

Collaborator

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?

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.

@mjackson mjackson closed this in 5ab345e Aug 31, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment