You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by "Harris, Bobby" <Bo...@CSX.com> on 2003/07/11 14:43:29 UTC

Resolution of looping variable

I'm having alot of fun with Velocity but have noticed one peculiar behavior
I'd like to get clarified if possible.

I have subclassed AbstractContext to suit my application requirements, I
noticed, however, that in the template

#foreach( $order2 in $event.order )
  order - $order2.number
    "Delivered?" - $order2.delivered
    "City?" - $order2.shippingaddress.city
#end


Velocity attempts to resolve $order2 in the context, eventhough it is only a
variable for looping.

Is the behaving as designed?

Thanks for your help.

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


Re: Resolution of looping variable

Posted by Claude Brisson <cl...@savoirweb.com>.
The behaviour of the foreach directive roughly is the following :

 - if there is already a $order2 variable in the context, save its value
 - at each iteration, put the next value in $order2
 - restore previous value of $order2 if there is one

I don't clearly understand your question, since Velocity has nowhere to store variables apart from the context, even loop variables,
but I hope this helps.

CloD

----- Original Message -----
From: "Harris, Bobby" <Bo...@CSX.com>
To: <ve...@jakarta.apache.org>
Sent: vendredi 11 juillet 2003 14:43
Subject: Resolution of looping variable


>
> I'm having alot of fun with Velocity but have noticed one peculiar behavior
> I'd like to get clarified if possible.
>
> I have subclassed AbstractContext to suit my application requirements, I
> noticed, however, that in the template
>
> #foreach( $order2 in $event.order )
>   order - $order2.number
>     "Delivered?" - $order2.delivered
>     "City?" - $order2.shippingaddress.city
> #end
>
>
> Velocity attempts to resolve $order2 in the context, eventhough it is only a
> variable for looping.
>
> Is the behaving as designed?
>
> Thanks for your help.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>


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


Re: Resolution of looping variable

Posted by Ch...@dlr.de.
Look at the implementation of the ForeachDirective in the sources,
you will see that it temporarily sets a variable into the context
to allow access within the loop.

This way the introspection mechanisms works on it just the same
(note that the iterated value may be a complex object with loads
of methods... no just an int or String!).

Harris, Bobby wrote:
> I'm having alot of fun with Velocity but have noticed one peculiar behavior
> I'd like to get clarified if possible.
> 
> I have subclassed AbstractContext to suit my application requirements, I
> noticed, however, that in the template
> 
> #foreach( $order2 in $event.order )
>   order - $order2.number
>     "Delivered?" - $order2.delivered
>     "City?" - $order2.shippingaddress.city
> #end
> 
> 
> Velocity attempts to resolve $order2 in the context, eventhough it is only a
> variable for looping.
> 
> Is the behaving as designed?

Yes.

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

-- 
:) Christoph Reck


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