You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Michał Tkacz <me...@autocom.pl> on 2004/09/25 22:26:40 UTC

[Newbie] #foreach and an array with null elements

Hi,

I have an array in my context over which I'd like to iterate using 
#foreach directive. Sometimes an element of the array is null, in which 
case I'd like to skip it. Unfortunately it seems that #foreach simply 
does not set the loop variable if the element is null. Thus I am 
completely unable to detect that situation.

Example:
I put an array into the context like this:
        context.put("array", new Object[] {new Integer(1), null, new 
Integer(3)});
My template looks like this:
        #foreach($item in $array)
            #if($item)
                $item
            #end
        #end
The result is:
1 1 3

Is there any workaround for this behaviour?

Thanks in advance

Michal Tkacz

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


Re: [Newbie] #foreach and an array with null elements

Posted by Nathan Bubna <na...@esha.com>.
Michał Tkacz said:
> Thanks for all the answers. Let me comment on these:
...
> >>2) You can access your array by index number.
> >>
> >>## not sure if this syntax is correct
> >>#foreach($index in [1...$array.length])
> >>
> >>#if ($array.get($index))
> >>#set ($item = $array.get($index))
> >>#else## it's null
> >>## handle null case
> >>#end##if
> >>
> >>#end##for
> >>
> >>
>
> I thought about it, unfortunately neither $array.length, nor
> $array.get($index) seems to work for arrays.
> Also [1...$var] syntax doesn't seem to be supported.
...

you're right.  [1...$var]  isn't supported.  but i'm pretty sure that
[1..$var] works.  ;)

Nathan Bubna
nathan@esha.com


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


Re: [Newbie] #foreach and an array with null elements

Posted by Michał Tkacz <me...@autocom.pl>.
Thanks for all the answers. Let me comment on these:

>>1) You can write a custom tool to somehow handle your situation.
>>    
>>

I'm not sure what you mean, but I guess I'd like to avoid it :)

>>2) You can access your array by index number.
>>
>>## not sure if this syntax is correct
>>#foreach($index in [1...$array.length])
>>
>>#if ($array.get($index))
>>#set ($item = $array.get($index))
>>#else## it's null
>>## handle null case
>>#end##if
>>
>>#end##for
>>    
>>

I thought about it, unfortunately neither $array.length, nor 
$array.get($index) seems to work for arrays.
Also [1...$var] syntax doesn't seem to be supported.

>>3) You can try to detect if the current element is the same as the previous
>>one
>>
>>    #set ($lastElement = 'nothing that appears in array')
>>        #foreach($item in $array)
>>             #if($item.equals($lastElement))
>>## it's probably null
>>#else
>>                ## non-null code
>>#set ($lastitem = $item)
>>             #end
>>         #end
>>    
>>

Although it could work in my case, it doesn't seem to be a good 
solution, as in general the array could contain equal values next to 
each other.

>>4) install a patch to velocity that allows you to #set things to null.
>>Might already be one in bugzilla.  If not, submit one. :)  There's a lot of
>>people interested in the feature.
>>    
>>

As I'm just starting, I guess I'll leave it to the more advanced 
users/developers of Velocity ;)

>5)  set the $item to false at the bottom of the loop:
>
>#foreach( $item in $array )
>    #if( $item ) $item #else no item #end
>    #set( $item = false )
>#end
>
>disclaimer:  i don't do this personally, but i've heard of it.
>  
>

I came with that idea during the night and it actually works! I will 
stick with that solution as it seems to be the simplest one.

Michal

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


Re: [Newbie] #foreach and an array with null elements

Posted by Nathan Bubna <na...@esha.com>.
Mike Kienenberger said:
> =?ISO-8859-2?Q?Micha=B3_Tkacz?= <me...@autocom.pl> wrote:
> > I have an array in my context over which I'd like to iterate using
> > #foreach directive. Sometimes an element of the array is null, in which
> > case I'd like to skip it. Unfortunately it seems that #foreach simply
> > does not set the loop variable if the element is null. Thus I am
> > completely unable to detect that situation.
> >
> > Example:
> > I put an array into the context like this:
> >         context.put("array", new Object[] {new Integer(1), null, new
> > Integer(3)});
> > My template looks like this:
> >         #foreach($item in $array)
> >             #if($item)
> >                 $item
> >             #end
> >         #end
> > The result is:
> > 1 1 3
> >
> > Is there any workaround for this behaviour?
>
> Yeah, unfortunately, velocity never lets you set an attribute to null.  It
> just retains the previous value.  Annoying.
>
> A few things you can try. {Syntax may be wrong as I'm doing these off the
> top of my head}
>
> 1) You can write a custom tool to somehow handle your situation.
>
> 2) You can access your array by index number.
>
> ## not sure if this syntax is correct
> #foreach($index in [1...$array.length])
>
> #if ($array.get($index))
> #set ($item = $array.get($index))
> #else## it's null
> ## handle null case
> #end##if
>
> #end##for
>
> 3) You can try to detect if the current element is the same as the previous
> one
>
>     #set ($lastElement = 'nothing that appears in array')
>         #foreach($item in $array)
>              #if($item.equals($lastElement))
> ## it's probably null
> #else
>                 ## non-null code
> #set ($lastitem = $item)
>              #end
>          #end
>
> 4) install a patch to velocity that allows you to #set things to null.
> Might already be one in bugzilla.  If not, submit one. :)  There's a lot of
> people interested in the feature.

5)  set the $item to false at the bottom of the loop:

#foreach( $item in $array )
    #if( $item ) $item #else no item #end
    #set( $item = false )
#end

disclaimer:  i don't do this personally, but i've heard of it.

Nathan Bubna
nathan@esha.com


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


Re: [Newbie] #foreach and an array with null elements

Posted by Mike Kienenberger <mk...@alaska.net>.
=?ISO-8859-2?Q?Micha=B3_Tkacz?= <me...@autocom.pl> wrote:
> I have an array in my context over which I'd like to iterate using 
> #foreach directive. Sometimes an element of the array is null, in which 
> case I'd like to skip it. Unfortunately it seems that #foreach simply 
> does not set the loop variable if the element is null. Thus I am 
> completely unable to detect that situation.
> 
> Example:
> I put an array into the context like this:
>         context.put("array", new Object[] {new Integer(1), null, new 
> Integer(3)});
> My template looks like this:
>         #foreach($item in $array)
>             #if($item)
>                 $item
>             #end
>         #end
> The result is:
> 1 1 3
> 
> Is there any workaround for this behaviour?

Yeah, unfortunately, velocity never lets you set an attribute to null.  It 
just retains the previous value.  Annoying.  

A few things you can try. {Syntax may be wrong as I'm doing these off the 
top of my head}

1) You can write a custom tool to somehow handle your situation.

2) You can access your array by index number.

## not sure if this syntax is correct
#foreach($index in [1...$array.length])

#if ($array.get($index))
	#set ($item = $array.get($index))
#else## it's null
	## handle null case
#end##if

#end##for

3) You can try to detect if the current element is the same as the previous 
one

	    #set ($lastElement = 'nothing that appears in array')
        #foreach($item in $array)
             #if($item.equals($lastElement))
				## it's probably null
			 #else
                ## non-null code
				#set ($lastitem = $item)
             #end
         #end

4) install a patch to velocity that allows you to #set things to null.   
Might already be one in bugzilla.  If not, submit one. :)  There's a lot of 
people interested in the feature.

-Mike

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