Discussion:
[FreeMarker-user] Short form of if..else
anjibman
2013-08-06 20:56:44 UTC
Permalink
Hi All,

What is the short form of
<#if myDTO.field??>
${myDTO.filed?size}
<#else>
0
<#/if>

Right now I have ${myDTO.field ! "0"} which *doesn't* print size if
myDTO.field is not NULL.





--
View this message in context: http://freemarker.624813.n4.nabble.com/Short-form-of-if-else-tp4654720.html
Sent from the freemarker-user mailing list archive at Nabble.com.
Denis Bredelet
2013-08-07 12:46:00 UTC
Permalink
Post by anjibman
Hi All,
What is the short form of
<#if myDTO.field??>
${myDTO.filed?size}
<#else>
0
<#/if>
Right now I have ${myDTO.field ! "0"} which *doesn't* print size if
myDTO.field is not NULL.
Do you mean you have ${myDTO.field?size ! 0} ?


-- Denis.
Daniel Dekany
2013-08-07 15:28:59 UTC
Permalink
Post by anjibman
Hi All,
What is the short form of
<#if myDTO.field??>
${myDTO.filed?size}
<#else>
0
<#/if>
Right now I have ${myDTO.field ! "0"} which *doesn't* print size if
myDTO.field is not NULL.
What your code really want to say is that if `field` doesn't exist,
then it should be treated like if its size was 0. So just say that in
FTL too:

${(myDTO.field![])?size}

Another solution is:

${(myDTO.field?size)!0}

but it's less telling, also it will hide the error if `myDTO` is null,
which is unintended.
--
Thanks,
Daniel Dekany
Loading...