Here are some requests for null support in Velocity. Here is the discussion about this on the developer list.
For example:
#set ($list = ["one", null, "three"]) |
should make a 3 element list, with the elements "one", null and "three".
$foo.bar(null) |
should call the "bar" method of the "foo" object with a null parameter.
Workarounds
#set ($list = ["one", $somevariablenamethatneverwillbeused, "three"]) |
$foo.bar($somevariablenamethatneverwillbeused) |
For example:
#if ($foo == null) foo is null #end |
should output
foo is null |
if foo was not in the Context.
#if ($foo != null) foo is not null #end |
should output
foo is not null |
if foo was in the Context.
Workarounds
#ifnull ($foo) foo is null #end |
#ifnotnull ($foo) foo is not null #end |
For example:
Assuming that $bar wasn't in the Context,
#set ($foo = $bar) #set ($foo = null) |
should both remove "foo" from the Context, and
#set($map.foo = $bar) #set($map.foo = null) |
should both set the "foo" property of "$map" to null.
Workarounds
$ctx.remove("foo") $map.put("foo", $bar) $map.setFoo(null) |
$null.setNull("foo") |
Currently, it preserves the last element.
For example:
#set ($list = ["one", null, "three"]) #foreach ($foo in $list) $!foo #end |
should output:
one three |
Workarounds
#foreach ($foo in $list) $!foo $null.setNull("foo") #end |
Comment from Nathan about the null keyword and the if directive support:
-0 i'm not totally against it, but i'm instinctively uncomfortable with using this programming concept in a template language. personally, i've always felt in a display-oriented language, null and the empty string should be totally equivalent. but that is already not the case, so i can't really be -1 on this.