Re: "While"-like structure in Velocity?

Christopher Schultz <[email protected]>
Newsgroups gmane.comp.jakarta.velocity.user
Message-ID <[email protected]>
Sergiu,

On 5/15/13 3:04 PM, Sergiu Dumitriu wrote:
> On 05/15/2013 12:11 PM, Christopher Schultz wrote:
>> All,
>>
>> I've searched the archives for "while" capabilities in Velocity, and
>> mostly the answers have been things like "that's beyond the scope of
>> Velocity... you want a real programming language for that".
>>
>> I'd like to do something that naturally would use a while loop, but
>> perhaps someone can offer an alternative suggestion.
>>
>> I need to traverse a queue, but that queue needs to be able to modify
>> itself during the processing. Basically, something like this:
>>
>> while (!queue.empty) {
>>   // do something with the head element
>>   // possible enqueue some more items at the back
>> }
>>
>> #foreach would be the way to do it if the queue didn't have to be
>> modified, but I'll get an exception if I try to traverse an iterator
>> over the queue and then modify it mid-flight.
>>
>> Right now, I have this written as a JSP but I'd rather have it as a
>> Velocity template.
>>
>> My only thought is to create an ugly class that implements Iterator and
>> wraps the queue but is much less strict than a standard iterator. Any
>> suggestions?
>>
>> -chris
>>
> 
> While not a very clean solution, you can use a very large foreach and
> manually work with the queue inside the loop, and #break($foreach) once
> the queue is empty:
> 
> #foreach($i in [0..100000])
>   #if ($queue.isEmpty())
>     #break($foreach)
>   #end
>   #set ($item = $queue.pop())
>   ## Do stuff with $item
>   #if (should add more things)
>     #set ($discard = $queue.add(new things))
>   #end
> #end

That's actually a great suggestion. I've never used "break" in a foreach
loop before. $foreach becomes the loop-control identifier for #break?

I'll give that a try. 10,000 seems completely reasonable given that (for
now) I'll never really have a queue that grows to a size more than about
5000, even in the most pathological case.

Thanks,
-chris
signature.asc (application/pgp-signature, 946 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBCAAGBQJRlPoKAAoJEBzwKT+lPKRYSTEP/24TBokeDQ6vnutO7ZVd4ihL
wh5AISwqY4S9YBQiNFJ8qK3MsEjL3Cgby7QmRdZFCuj97BmMT9bDmBI8ojJ4w1xJ
6+KRd1TAOIzEaEOY42Vh5p+Nrut/MD1mMgxOj+nzBceLwqVHqlQqG01WePzTM2Ib
+jVbDum5wUCstOaWo9QetrzXKP3GvaFHBOSUlLuMxnGN/BMLN2fIZCKkNz6CFv31
qMLU/qGqseAnc2BkyTNAVs9j0GigCr30X8CxycootVRZ1pEUvSpJrgkqU4w9POCa
nY2ciKy7d3zPz1qJD0+ZyhvvzpNnfcHKB0vIcuMywgpvC5Kdym9FPASBfzF4Oab+
MLAiBS9PzHUbIKCtkINmmpnSYbUQrljXut9kcIF0rA01phHzAyF1j8P95GdOLhyU
IvxCjqZ5djH7286H95FyI1wyAyW2j5ZaUY6PnSPqtLg1+PqGyrpp6GEb2DrxSlyV
xwG0moXm72pcLaapl9Wgjl/PAHUuSqgrp7oOgrfTRYY5GsMpXyOy5tzNJf53Sq1p
+ICMU7qJQ5eWCrpoQ7BdBddfHKTDcqY8pRTNqbURKyn3udC4gPOqq6RrWi0QMotJ
73FbXdG+ab0FQmrRHNCH2cZlCmNAomLAPa0l7E019Mo0FOoC60vZJI8YPm3kNkGK
Fso1ulHN1G7VDauzxfnx
=IYAI
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.