When the innards of the $val block ($item.get(1)) resolve to null,
they are output as they were.  So $val should render as the literal
string "$item.get(1)".  That string is definitely not null.   If you
want, you can use quiet notation ($!item.get(1)), but even that will
render as an empty string "", which is also not null.  So, no matter
what, your $val reference will not be null.  It is a defined block
object which never renders as null.  It is not a workable shortcut for
null testing.  You can do #if( $val == "" ) if you switch to quiet
notation or #if( $val == "$item.get(1)" ) to test properly as things
are.

On Wed, Oct 14, 2009 at 3:35 AM, Margus Kliimask
<margus.kliim...@mindworks.ee> wrote:
> Hi,
> I've run into a problem with testing for a null in #defined block with
> an #if directive. The manual states: "The variable $foo is evaluated
> to determine whether it is true, which will happen under one of two
> circumstances: <..> or (ii) the value is not null.
>
> I feed an array of lists
> ...
> String[] [] Items = {{"A", "100"}, {"B", null}};
> context.put("Items", Items);
> ...
>
> to this Velocity script:
> ...
> #define( $key )$item.get(0)#end
> #define( $val )$item.get(1)#end
>
> #foreach ( $item in $Items )
> (1)#if( $item.get(1) )$key:$val
> #end
> (2)#if( $val )$key:$val
> #end
>
> #end
> ...
>
> I get back:
> (1)A:100
> (2)A:100
>
> (1)(2)B:$item.get(1)
>
> So, #if could detect null (missing value) in #if( $item.get(1) ), but
> could not in #defined block #if( $val ). What am I missing? I
> definitely don't want to mention the b-word here :) and appreciate any
> help, guidance or comments.
>
> Thanks,
> Margus
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscr...@velocity.apache.org
> For additional commands, e-mail: user-h...@velocity.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@velocity.apache.org
For additional commands, e-mail: user-h...@velocity.apache.org

Reply via email to