Re: Handling null objects

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Friday, April 4, 2008, 7:53:48 PM, mateamargo wrote:

> I haven't found in the doc how to handle the null objects.

The Manual says that a variable whose value us null and missing
variable is the same under FreeMarker 2.3.x. (This will be different
in FreeMarker 2.4, however, where if you want to, you can tell the
difference.)

> I load from the database a user by its id, but it may happen that doesn't
> exist. In that case, the "user" variable will be null.
>
> I wanted to evaluate it in a if:
>
> <#if user == null>

That should be: <#if user??>
This means "if the user variable doesn't exist".

>         New User
> <#else>
>         Editing <em>${user.name}</em>
> </#if>
>
> But I get this error:
>
> Expression null is undefined

That's because FreeMarker 2.3.x "null" is the name of a variable, as
it doesn't know it as a keyword. (FreeMarker 2.4 will, but you still
can't just write user == null, but user?is_null, as *value* comparison
with null is nonsense.)

> How should I evaluate that object?

-- 
Best regards,
 Daniel Dekany


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
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.