Discussion:
question about loops
Greg Frohn
2002-08-05 21:00:12 UTC
Permalink
I've been using velocity for a while now, and for the most part I love
it. I'm wondering why are there no for/while loops for VTL?

The foreach loop can be a bit limiting...especially since VTL doesn't
seem to support break or continue.

I'd love for our web developers to be able to do simple things like
print out the first n elements in a List. I understand that this can be
done using range operators and get methods, but having
while/for/break/continue seems to be much cleaner for many tasks.

Just wondering what your thoughts/reasons are for not including them.

Anyway, congrats and thanks for a great project.

Greg
***@reliacast.com
Jason van Zyl
2002-08-04 21:36:57 UTC
Permalink
Post by Greg Frohn
I've been using velocity for a while now, and for the most part I love
it. I'm wondering why are there no for/while loops for VTL?
The foreach loop can be a bit limiting...especially since VTL doesn't
seem to support break or continue.
I'd love for our web developers to be able to do simple things like
print out the first n elements in a List. I understand that this can be
done using range operators and get methods, but having
while/for/break/continue seems to be much cleaner for many tasks.
Just wondering what your thoughts/reasons are for not including them.
We originally cloned Webmacro and it only had a foreach so we only had a
foreach. But we also tried not to make Velocity a scripting language as
we had designers in mind (even though Velocity has been used in
everything from mailing list managers to source generators). People
asked for while/for loops long ago and occasionally ask and really we
were just trying to keep it simple.
Post by Greg Frohn
Anyway, congrats and thanks for a great project.
Greg
--
jvz.

Jason van Zyl
***@apache.org
http://tambora.zenplex.org

In short, man creates for himself a new religion of a rational
and technical order to justify his work and to be justified in it.

-- Jacques Ellul, The Technological Society
Geir Magnusson Jr.
2002-08-06 10:05:10 UTC
Permalink
Post by Greg Frohn
I've been using velocity for a while now, and for the most part I love
it. I'm wondering why are there no for/while loops for VTL?
The foreach loop can be a bit limiting...especially since VTL doesn't
seem to support break or continue.
I'd love for our web developers to be able to do simple things like
print out the first n elements in a List. I understand that this can be
done using range operators and get methods, but having
while/for/break/continue seems to be much cleaner for many tasks.
Just wondering what your thoughts/reasons are for not including them.
The problem we are trying to avoid is runaway loops :

#while(true)
...
#end

Or

#for( $I= 0; $I < 10; $I = $I -1 )
...
#end

With a foreach, it's awfully hard to make simple mistakes that lead to these
kinds of problems.
--
Geir Magnusson Jr.
Research & Development, Adeptra Inc.
***@adeptra.com
+1-203-247-1713
Denis
2002-08-06 15:09:09 UTC
Permalink
Post by Geir Magnusson Jr.
#while(true)
...
#end
Or
#for( $I= 0; $I < 10; $I = $I -1 )
...
#end
With a foreach, it's awfully hard to make simple mistakes that
lead to these
kinds of problems.
With foreach, there can't be infinite loops except for badly
programmed iterators. But that shouldn't stop the template
designers to implement a loop limited to 10 items...

If I'd wanted to display 10 first items of a list, I would like to
be able to do something like:


#set ($indexes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
#foreach ($index in $indexes)
#display( $items[index] )
#end


Or with the #break directive:


#foreach ($item in $items)
#display( $item )
#if ($velocityCount = 10)
#break
#end
#end


There was also the proposal of the composed foreach:


#set ($indexes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
#foreach ($index in $indexes, $item in $items)
#display( $item )
#end


-- Denis.
Post by Geir Magnusson Jr.
--
Geir Magnusson Jr.
Research & Development, Adeptra Inc.
+1-203-247-1713
Nathan Bubna
2002-08-06 15:12:23 UTC
Permalink
Denis said:
...
Post by Denis
If I'd wanted to display 10 first items of a list, I would like to
#set ($indexes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
#foreach ($index in $indexes)
#display( $items[index] )
#end
you can do this easily. just use the short syntax for defining an integer
array "[x..y]" and call get(int) on your list of items...

#foreach( $index in [1..10] )
#display( $items.get($index) )
#end

Nathan Bubna
***@esha.com
Geir Magnusson Jr.
2002-08-06 15:43:47 UTC
Permalink
Post by Nathan Bubna
...
Post by Denis
If I'd wanted to display 10 first items of a list, I would like to
#set ($indexes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
#foreach ($index in $indexes)
#display( $items[index] )
#end
you can do this easily. just use the short syntax for defining an integer
array "[x..y]" and call get(int) on your list of items...
#foreach( $index in [1..10] )
#display( $items.get($index) )
#end
Yep
--
Geir Magnusson Jr.
Research & Development, Adeptra Inc.
***@adeptra.com
+1-203-247-1713
Loading...