You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Martin Roller <Ma...@dynaware.de> on 2003/09/06 14:06:40 UTC
Distinguishing null and FALSE
I would like to write a macro that renders a java.lang.Boolean value with
the aid of some Locator object (which localizes Strings). I am having
difficulties to distinguish a null object from Boolean.FALSE. What I came up
with was a wonderfully disgusting hack:
#macro ( renderBoolean $value $locator)
#if ( !$value.toString() )
$locator.localize('undefined')
#elseif ( $value )
$locator.localize('true')
#else
$locator.localize('false')
#end
#end
Is there a cleaner way of doing this?
Martin Roller,
Dynaware
Re: Distinguishing null and FALSE
Posted by Nathan Bubna <na...@esha.com>.
Martin Roller said:
> I would like to write a macro that renders a java.lang.Boolean value with
> the aid of some Locator object (which localizes Strings). I am having
> difficulties to distinguish a null object from Boolean.FALSE. What I came up
> with was a wonderfully disgusting hack:
>
> #macro ( renderBoolean $value $locator)
> #if ( !$value.toString() )
> $locator.localize('undefined')
> #elseif ( $value )
> $locator.localize('true')
> #else
> $locator.localize('false')
> #end
> #end
>
> Is there a cleaner way of doing this?
can you not change the localize method to expect and empty string for null?
then you could just do
$locator.localize( "$!value" )
if you can't, well, i sometimes use an #altnull() macro for things:
#macro( altnull $variable $alternate )
#if( "$!variable" == "" )$!alternate#else$variable#end##
#end
with that you can do:
$locator.localize( "#altnull( $value 'undefined' )" )
Nathan Bubna
nathan@esha.com
Re: Distinguishing null and FALSE
Posted by Nathan Bubna <na...@esha.com>.
Martin Roller said:
> I wrote
> >> There remains the feeling that in Velocity it is hard to distinguish
> >> - null values
> >> - method calls that throw exceptions
> >> - boolean false
> >> - Boolean.FALSE
you're not generally supposed to distinguish between false and Boolean.FALSE
in velocity *by design.* that would be considered something that doesn't
really belong in view logic and is thus intentionally unsupported.
...
> Shure, I appreciate the succinctness of #if($ref),
> what I'm missing is a way to express null or exception.
> If I can write $value == false, why can't I write value == null?
> (Velocity says: Encountered "null" at line ..., column ...)
there's was talk in the [fairly distant] past about adding some sort of NULL
token to VTL, and i vaguely recall some tacit agreement to do so, but it's
never happened so far as i know. i don't recall why.
> And, while we're at it, why can't I write false.equals( $value )?
> (Velocity says: Encountered: "e" (101), after : ".")
not sure. but if you really want, you can probably do:
#set( $false = false )
#if ( $false.equals( $value ) )
or, if using velocity-tools, add the $false variable automatically and
globally via the toolbox: <data
type="boolean"><key>false</key><value>false</value></data>
> Btw: I only ask those questions to understand the design of
> the velocity script language. I use Velocity in a project and think
> it is currently the best tool for my purpose. And I do like parsers
> that tell you where they think you made a mistake!
last i recall, folks around here were still opposed to calling Velocity a
"script language." it's the "Velocity Template Language," meant for
templating, not scripting.
Nathan Bubna
nathan@esha.com
AW: Distinguishing null and FALSE
Posted by Martin Roller <Ma...@dynaware.de>.
I wrote
>> There remains the feeling that in Velocity it is hard to distinguish
>> - null values
>> - method calls that throw exceptions
>> - boolean false
>> - Boolean.FALSE
CloD replied
>That's a trade-off, to be able to write something like :
>#if( $ref )
>to check the existence of $ref
>
>Also, there are event cartridges that let you trap null references from
java.
Shure, I appreciate the succinctness of #if($ref),
what I'm missing is a way to express null or exception.
If I can write $value == false, why can't I write value == null?
(Velocity says: Encountered "null" at line ..., column ...)
And, while we're at it, why can't I write false.equals( $value )?
(Velocity says: Encountered: "e" (101), after : ".")
Btw: I only ask those questions to understand the design of
the velocity script language. I use Velocity in a project and think
it is currently the best tool for my purpose. And I do like parsers
that tell you where they think you made a mistake!
Martin
Re: Distinguishing null and FALSE
Posted by Claude Brisson <cl...@savoirweb.com>.
Martin Roller wrote :
> Thanks for your three suggestions so far:
you're welcome.
> There remains the feeling that in Velocity it is hard to distinguish
> - null values
> - method calls that throw exceptions
> - boolean false
> - Boolean.FALSE
That's a trade-off, to be able to write something like :
#if( $ref )
to check the existence of $ref
Also, there are event cartridges that let you trap null references from java.
CloD
----- Original Message -----
From: "Martin Roller" <Ma...@dynaware.de>
To: "'Velocity Users List'" <ve...@jakarta.apache.org>
Sent: mardi 9 septembre 2003 11:01
Subject: AW: Distinguishing null and FALSE
> Thanks for your three suggestions so far:
>
> 1. Alter/wrap the Java code for locator.
>
> Well, I could do that, but I don't want to.
> I want to know how to distinguish nulls and FALSE in Velocity.
>
> 2. Use "$!variable" == "" to recognize nulls.
>
> Somehow I consider that just as hackish.
> Also, this appears to rely on the fact the Velocity ==
> means Java equals(). Is this a fact?
> I couldn't find it in the User or Developer Documentation.
>
> 3. Use
>
> #if ($value)
> ...true...
> #elseif ($value == false)
> ...false...
> #else
> ...undefined...
> #end
>
> This works and satifies my curiosity!
>
> There remains the feeling that in Velocity it is hard to distinguish
> - null values
> - method calls that throw exceptions
> - boolean false
> - Boolean.FALSE
>
> Martin Roller
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>
Re: AW: Distinguishing null and FALSE
Posted by Ch...@dlr.de.
Hi,
Martin Roller wrote:
> Thanks for your three suggestions so far:
[snip]
> There remains the feeling that in Velocity it is hard to distinguish
> - null values
With the code as Claude suggested a differentiation is possible (it
seems you are already happy on his approach).
> - method calls that throw exceptions
Yup, you only can intercepot such excections with the EventCartrige
using code, not with the template. Maybe we could design a
configurable event cartrige and a corresponding directive to
simulate a try/catch block (by having the cartrige define a
specific context variable to allow a post-event-handling in the
template).
This needs some thoughts, bright ideas and a [PROPOSAL].
> - boolean false
> - Boolean.FALSE
As long as the security sandbox is not configured to avoid accessing
the getClass() method, you can do:
#if( "$!foo" == "false" )
#if( $foo.class.name == "boolean" )
we got a primitive boolean false value!
#elsif( $foo.class.name == "java.lang.Boolean" )
we got a java.lang.Boolean.FALSE value!
#end
#end
>
> Martin Roller
So overall, velocity is almost there!
Please keep your heads up!
--
:) Christoph Reck
AW: Distinguishing null and FALSE
Posted by Martin Roller <Ma...@dynaware.de>.
Thanks for your three suggestions so far:
1. Alter/wrap the Java code for locator.
Well, I could do that, but I don't want to.
I want to know how to distinguish nulls and FALSE in Velocity.
2. Use "$!variable" == "" to recognize nulls.
Somehow I consider that just as hackish.
Also, this appears to rely on the fact the Velocity ==
means Java equals(). Is this a fact?
I couldn't find it in the User or Developer Documentation.
3. Use
#if ($value)
...true...
#elseif ($value == false)
...false...
#else
...undefined...
#end
This works and satifies my curiosity!
There remains the feeling that in Velocity it is hard to distinguish
- null values
- method calls that throw exceptions
- boolean false
- Boolean.FALSE
Martin Roller
Re: Distinguishing null and FALSE
Posted by Claude Brisson <cl...@savoirweb.com>.
maybe a slightly cleaner one would be :
#if ($value)
...true...
#elseif ($value == false)
...false
#else
...undefined...
#end
CloD
----- Original Message -----
From: "Martin Roller" <Ma...@dynaware.de>
To: <ve...@jakarta.apache.org>
Sent: samedi 6 septembre 2003 14:06
Subject: Distinguishing null and FALSE
> I would like to write a macro that renders a java.lang.Boolean value with
> the aid of some Locator object (which localizes Strings). I am having
> difficulties to distinguish a null object from Boolean.FALSE. What I came up
> with was a wonderfully disgusting hack:
>
> #macro ( renderBoolean $value $locator)
> #if ( !$value.toString() )
> $locator.localize('undefined')
> #elseif ( $value )
> $locator.localize('true')
> #else
> $locator.localize('false')
> #end
> #end
>
> Is there a cleaner way of doing this?
>
> Martin Roller,
> Dynaware
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>