You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Christopher Schultz <ch...@christopherschultz.net> on 2021/01/05 15:40:06 UTC

Test variable for falsiness without generating a warning

All,

I've got a template where a variable may be either undefined or false 
(or other values) and I'm specifically trying to determine if it's 
*false*. My template code looks like this:

#if($value && true == $value)
do stuff
#elseif(false == $value)
do other stuff
#end

I don't seem to be able to get rid of the warning in the log file if 
$value is actually null.

Is it possible? Typically, you'd do something like:


#if($value)
stuff when value is defined and/or truthy
#else
stuff when value is undefined and/or falsey
#end

But I'm interested in telling the difference between (boolean) false and 
'undefined'.

Any ideas? Or should I just live with the log message? In production, 
these things don't generate logs, but in development they do. I'd just 
like to reduce the noise in dev if possible.

-chris

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


Re: Test variable for falsiness without generating a warning

Posted by Greg Huber <gr...@gmail.com>.
I would do this

##set($value=true)
#if($value && true == $value)
do stuff
#elseif(!$value || false == $value)
do other stuff

#end


On 05/01/2021 15:40, Christopher Schultz wrote:
> All,
>
> I've got a template where a variable may be either undefined or false 
> (or other values) and I'm specifically trying to determine if it's 
> *false*. My template code looks like this:
>
> #if($value && true == $value)
> do stuff
> #elseif(false == $value)
> do other stuff
> #end
>
> I don't seem to be able to get rid of the warning in the log file if 
> $value is actually null.
>
> Is it possible? Typically, you'd do something like:
>
>
> #if($value)
> stuff when value is defined and/or truthy
> #else
> stuff when value is undefined and/or falsey
> #end
>
> But I'm interested in telling the difference between (boolean) false 
> and 'undefined'.
>
> Any ideas? Or should I just live with the log message? In production, 
> these things don't generate logs, but in development they do. I'd just 
> like to reduce the noise in dev if possible.
>
> -chris
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>

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


Re: Test variable for falsiness without generating a warning

Posted by Andy Brook <ja...@gmail.com>.
Use the fuzzy match, its cumbersome in some cases as it can involve
repeating the same operation, your case, easy:

#if ($!value && true == $value)
do stuff
#else
...

The user guide doesn't really pull that out in the "if / else" block, look
for:

this is $!foo   ## renders to "this is " without an exception


On Tue, 5 Jan 2021 at 15:40, Christopher Schultz <
chris@christopherschultz.net> wrote:

> All,
>
> I've got a template where a variable may be either undefined or false
> (or other values) and I'm specifically trying to determine if it's
> *false*. My template code looks like this:
>
> #if($value && true == $value)
> do stuff
> #elseif(false == $value)
> do other stuff
> #end
>
> I don't seem to be able to get rid of the warning in the log file if
> $value is actually null.
>
> Is it possible? Typically, you'd do something like:
>
>
> #if($value)
> stuff when value is defined and/or truthy
> #else
> stuff when value is undefined and/or falsey
> #end
>
> But I'm interested in telling the difference between (boolean) false and
> 'undefined'.
>
> Any ideas? Or should I just live with the log message? In production,
> these things don't generate logs, but in development they do. I'd just
> like to reduce the noise in dev if possible.
>
> -chris
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>

-- 
Andy Brook