Re: Handling null values

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Friday, August 15, 2008, 3:42:05 PM, Daniel Dekany wrote:

> Friday, August 15, 2008, 2:23:17 PM, koevet wrote:
>
>> Hello, I have just started using Freemarker.
>>
>> I have a basic problem. How do I set the template to handle null values of
>> an int field of a pojo as String and display no values? 
>> Currently my template looks like:
>>
>> Event: "${event.eventSource!""}","${event.eventType!""}
>>
>> eventType is a int field of a pojo. If the field is null, Freemarker
>> displays 0 (default for int). I would like to change the behavior to display
>> an empty String. And no, I can't change the type to Integer.
>
> The ${event.eventSource!""} expression clearly tells FreeMarker that
> the default value is an empty string, not 0. Also, FreeMarker nowhere
> uses 0 as default for numbers. So, I guess, the interpretation of the
> missing value as 0 happens on some other level.

Oh, sorry, I have just realized what did you talk about... There is no
built-in stuff for that, but you could add a #function or
TemplateMethodModelEx for this, like:

  <#function hide0 x>
    <#if x == 0>
      <#return "">
    <#else>
      <#return x>
    </#if>
  </#function>

Sot then you can write:

  ${hide0(event.eventSource)}

-- 
Best regards,
 Daniel Dekany


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.