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 don't render with null values #40

Open
timbod7 opened this issue Mar 13, 2019 · 7 comments
Open

Inverted sections don't render with null values #40

timbod7 opened this issue Mar 13, 2019 · 7 comments

Comments

@timbod7
Copy link

timbod7 commented Mar 13, 2019

In other mustache implementations, a value of null triggers an inverted section. However in this one, that is not that case.

This would work, I think, if the code was changed to:

-- substituting an inverted section
substituteNode (InvertedSection  Implicit _) = tellError InvertedImplicitSection
substituteNode (InvertedSection (NamedData secName) invSecSTree) =
  search secName >>= \case
    Just (Bool False) -> contents
    Just Null -> contents                                            -- <<< new case
    Just (Array a)    | V.null a -> contents
    Nothing           -> contents
    _                 -> return ()
  where
    contents = mapM_ substituteNode invSecSTree

However, to my surprise, it seems that the standard (https://mustache.github.io/mustache.5.html) does not specify this:

While sections can be used to render text one or more times based on the value of the key, inverted sections may render text once based on the inverse value of the key. That is, they will be rendered if the key doesn't exist, is false, or is an empty list.

@JustusAdam
Copy link
Owner

Which implementations specifically? If this is not in the standard I don't feel comfortable including it here unless it really is a standard feature of a significant majority of mustache implementations.

@timbod7
Copy link
Author

timbod7 commented Mar 13, 2019

I happened to be depending on the cli tool in https://github.com/mustache/mustache.

Which standard is this implementation conforming to? The quote above says "is false", but that could mean "is the json false value" or "is falsey in according to the source language".

This spec https://github.com/mustache/spec refers to falsey.

@JustusAdam
Copy link
Owner

Version 1.1.3, which as of this comment is still current.

My travis setup for this repository actually pulls the spec you are linking to and tests against their test suite.

@timbod7
Copy link
Author

timbod7 commented Mar 17, 2019

This spec:

https://github.com/mustache/spec/blob/master/specs/inverted.yml

doesn't have any test cases for what I am describing. However, it does state this:

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.

Whilst this is language specific and a bit vague, I'm not aware of any language where a json null value is considered truthy.

@foxbenjaminfox
Copy link

I too was just now bitten by this issue. It seems more than a bit strange that null neither renders an inverted section nor a non-inverted section, meaning that there is seemingly no way to get a proper if/else effect.

For instance:

{{#value}} The value exists! {{/value}}
{{^value}} No, it doesn't exist. {{/value}}

It seems that one of the two lines should be rendered. But if value is null, that is not the case!

I'll also point out that in both the original ruby implementation and in mustache.js, null values render inverted sections.

Note too that by a strict reading of the spec there appears no requirement that null value causes a (non-inverted) section not to render. But if you've decided (very reasonably) to interpret the spec to mean that a null value makes a section not render, for the sake of consistency between sections and inverted sections it only makes sense if a null in an inverted section does cause it to render. For a null to cause neither a section nor an inverted section to render not makes it very difficult practically to use null values in mustache templates, it also seems to lack in consistency.

@TheInnerLight
Copy link
Contributor

The spec has now been updated to cover this case (https://github.com/mustache/spec/blob/master/specs/inverted.yml#L49).

 - name: Null is falsey
    desc: Null is falsey.
    data: { "null": null }
    template: '"{{^null}}This should be rendered.{{/null}}"'
    expected: '"This should be rendered."'

I will submit a PR to make this change.

@JustusAdam
Copy link
Owner

Somehow I missed a lot of this discussion. Sorry for the absence. I'll review the PR in a bit.

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

4 participants