LinkedInAttic / dustjs-helpers

Helpers for dustjs-linkedin
MIT License
114 stars 70 forks source link

Boolean types not supported for select #29

Closed mic0331 closed 12 years ago

mic0331 commented 12 years ago

hello,

I have this model :

{
    "current": false
}

when i try to apply this dust template the output is empty :

{#current}
  {@select key="{current}"}
    {@eq value=false}
      bbbbb
    {/eq}
    {@default}
      aaaaa
    {/default}
  {/select}
{/current}

If i simply add quotes to /false/ it works...

{
"current": "false"
}

and the template :

{@eq value="false"}

output --> bbbbb

Is there something wrong with my code ? Does "select" support "pure" boolean types (boolean recorded with no quotes) ?

thanks for your input

vybs commented 12 years ago

@mic0331 please read this section https://github.com/linkedin/dustjs/wiki/Dust-Tutorial#wiki-Logic_in_Templates

boolean false is falsy.

and any non empty string is truthy

So if current = false, dust will not even enter the {#current} block

If curious, see the dust.isEmpty method is the dust-core.js on how false/truth values are evaluated

vybs commented 12 years ago

BTW, the above template can be just simplified to

{?current} bbbbb {/eq} {:else} aaaaa {/current}

Any reason we need all these helpers?

mic0331 commented 12 years ago

Yes you are right this template is much simple to read and the helper is probably useless for what i m trying to do... but what I still have some issue to understand how I can evaluate a true/false value?

E.g.

If I use the model:

{
  "Current":false
}

Or

{
  "Current":true
}

The value is a Boolean not a string. The output will be always aaaaa

{?current}
bbbbb
{:else}
aaaaa
{/current}

Thanks for your help and sorry if my question is a dumb question...

vybs commented 12 years ago

No problem, it took me a while to understand all the truthy and falsy.

We have unit tests here to play around with : http://linkedin.github.com/dustjs/test/test.html

Have you tried it?

Current is upper case C, so in this case since it does not find the element in the JSON, it will always evaluate to false ( hence the else case )

mic0331 commented 12 years ago

Oh sorry I didn't notice upper case C ... Sorry for the waste of time it is now working for me. I will definitively spend more time in the documentation..

I actually played with the tester page but did not see the error...

Have a great day.