velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jonathan Revusky <jrevu...@terra.es>
Subject Re: How to find out looping reference
Date Fri, 11 Oct 2002 21:29:32 GMT
Alan Chen wrote:

> Hi,
>
> I am using the templatetool in the contrib/temporary directory
> to get all the references in a template. But I need find out
> if a reference is only used in #foreach loop.
>
> For example:
>
> #foreach ($item in $itemlist)
>   $item
> #end
>
> How can I find out $item is the looping reference?


Alan, I don't think there is any way of dealing with this in Velocity, 
it seems, because of the lack of local variables. There's no notion of 
whether a variable is local to a loop.

If you were using Freemarker, there would be a solution. Though I don't 
know for sure what you need. But broadly speaking, if you consider the 
following snippet:

<foreach item in itemlist>
   (The variable item is local to the loop.)

   <if root.item?exists>
      (This block only will be entered if there is a variable defined 
outside
      the loop called item.)

   </if>
</foreach>

In the above, it is possible that there is a variable called item that 
is defined globally in the template. However, that variable is hidden by 
the loop-local variable "item" in the foreach directive, right?

But FM gives you a way to get at the global "item" variable, via 
root.item. So, if you want to query whether the variable item is defined 
outside the loop, you can use the <if root.item?exists> condition. (This 
is new BTW in FreeMarker 2.1. It's not available in earlier versions.)

>
>
> Any help much appreciated.


Well, I can only tell you how to deal with this in FreeMarker. I 
wouldn't know how to deal with this in FreeMarker. Though, actually, 
AFAIK, and anybody can correct me if I'm wrong, Velocity has no 
disposition for dealing with this issue.

Well, maybe that is helpful. I hope so.

Best Regards,

Jonathan Revusky

--
FreeMarker-Velocity comparison doc
http://freemarker.sourceforge.net/fmVsVel.html
Velocity->FreeMarker template conversion utility
http://freemarker.sourceforge.net/usCavalry.html








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




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


Mime
View raw message