You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Piyush Purang <pp...@gmail.com> on 2006/03/16 13:31:29 UTC

#foreach and scope of variables

Hi,

I have a foreach loop in my template. I set a variable 'col-size' and then
test if it's length > 0 to try and determine if it exists or not.

The problem is that the condition evaluates true if in the previous
iteration col-size.length() was greater than 0 i.e. col-size retains the
value from the previous iteration..

Is that intended? If yes then is there a way of changing the behaviour.

Or is it a faulty template?

Here is the code (it is part of a template that reads an XML file and
outputs an SQL dump file)


#foreach($col in $cols)
        #set($col-name=$col.getAttribute($col-attr-name).getValue())
        #set($col-type=$col.getAttribute($col-attr-type).getValue())

#set($col-size=$col.getAttribute($col-attr-size).getValue())
## the variable of interest...

#set($col-percision=$col.getAttribute($col-attr-percision).getValue())
        #set($col-not-null=$col.getAttribute($col-attr-not-null).getValue())
        #set($col-default=$col.getAttribute($col-attr-default).getValue())
        $col-name $col-type #if($col-size.length() > 0)($col-size#if($col-
percision.length() > 0),$col-percision#end)#end
#if($col-default.length()>0)DEFAULT
$col-default#end #if($col-not-null)NOT NULL#end,

#end  ##ends foreach


Thanks for clarifying things.

Cheers
Piyush

Re: #foreach and scope of variables

Posted by Piyush Purang <pp...@gmail.com>.
Thanks. Will do that.

On 3/16/06, Nathan Bubna <nb...@gmail.com> wrote:
>
> Yes, this behavior is to be expected.  Unlike java, Velocity does not
> create a new scope/namespace for each iteration in a #foreach loop.
> so if col-size is set to value A in the first iteration, then it will
> still have value A in the subsequent iterations until it is set again.
>
> so, just try doing a #set( $col-size = false ) or something like that
> at the end of every iteration.
>
> On 3/16/06, Piyush Purang <pp...@gmail.com> wrote:
> > Hi,
> >
> > I have a foreach loop in my template. I set a variable 'col-size' and
> then
> > test if it's length > 0 to try and determine if it exists or not.
> >
> > The problem is that the condition evaluates true if in the previous
> > iteration col-size.length() was greater than 0 i.e. col-size retains the
> > value from the previous iteration..
> >
> > Is that intended? If yes then is there a way of changing the behaviour.
> >
> > Or is it a faulty template?
> >
> > Here is the code (it is part of a template that reads an XML file and
> > outputs an SQL dump file)
> >
> >
> > #foreach($col in $cols)
> >         #set($col-name=$col.getAttribute($col-attr-name).getValue())
> >         #set($col-type=$col.getAttribute($col-attr-type).getValue())
> >
> > #set($col-size=$col.getAttribute($col-attr-size).getValue())
> > ## the variable of interest...
> >
> > #set($col-percision=$col.getAttribute($col-attr-percision).getValue())
> >
> #set($col-not-null=$col.getAttribute($col-attr-not-null).getValue())
> >
> #set($col-default=$col.getAttribute($col-attr-default).getValue())
> >         $col-name $col-type #if($col-size.length() >
> 0)($col-size#if($col-
> > percision.length() > 0),$col-percision#end)#end
> > #if($col-default.length()>0)DEFAULT
> > $col-default#end #if($col-not-null)NOT NULL#end,
> >
> > #end  ##ends foreach
> >
> >
> > Thanks for clarifying things.
> >
> > Cheers
> > Piyush
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>

Re: #foreach and scope of variables

Posted by Nathan Bubna <nb...@gmail.com>.
Yes, this behavior is to be expected.  Unlike java, Velocity does not
create a new scope/namespace for each iteration in a #foreach loop. 
so if col-size is set to value A in the first iteration, then it will
still have value A in the subsequent iterations until it is set again.

so, just try doing a #set( $col-size = false ) or something like that
at the end of every iteration.

On 3/16/06, Piyush Purang <pp...@gmail.com> wrote:
> Hi,
>
> I have a foreach loop in my template. I set a variable 'col-size' and then
> test if it's length > 0 to try and determine if it exists or not.
>
> The problem is that the condition evaluates true if in the previous
> iteration col-size.length() was greater than 0 i.e. col-size retains the
> value from the previous iteration..
>
> Is that intended? If yes then is there a way of changing the behaviour.
>
> Or is it a faulty template?
>
> Here is the code (it is part of a template that reads an XML file and
> outputs an SQL dump file)
>
>
> #foreach($col in $cols)
>         #set($col-name=$col.getAttribute($col-attr-name).getValue())
>         #set($col-type=$col.getAttribute($col-attr-type).getValue())
>
> #set($col-size=$col.getAttribute($col-attr-size).getValue())
> ## the variable of interest...
>
> #set($col-percision=$col.getAttribute($col-attr-percision).getValue())
>         #set($col-not-null=$col.getAttribute($col-attr-not-null).getValue())
>         #set($col-default=$col.getAttribute($col-attr-default).getValue())
>         $col-name $col-type #if($col-size.length() > 0)($col-size#if($col-
> percision.length() > 0),$col-percision#end)#end
> #if($col-default.length()>0)DEFAULT
> $col-default#end #if($col-not-null)NOT NULL#end,
>
> #end  ##ends foreach
>
>
> Thanks for clarifying things.
>
> Cheers
> Piyush
>
>

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