For index loops in ColdFusion that are looping over an array or list, I always put the ArrayLen() or ListLen() in the TO attribute. I always felt a little uneasy about it because I thought maybe the function was getting evaluated for each iteration of the index loop. My fear was that this was adding additional processing overhead for each iteration.
Well, I finally got my out of shape butt off the proverbial couch and actually tested what was going on:
- <!--- Create an initial array of items. --->
- <cfset arrItems = ListToArray( "ben,dave,mark,mike" ) />
- Original Length: #ArrayLen( arrItems )#<br />
- <br />
- Loop over the items in the array. For each iteration, add
- an item to the array to see if the loop will keep going.
- In the TO attribute, use the Min() method to ensure that
- the loop doesn't go on infinitely.
- to="#Min( 10, ArrayLen( arrItems) )#"
- <!--- Output the iteration values. --->
- Iteration #intIndex#. Array Length: #ArrayLen( arrItems )#<br />
- <!--- Add an Item to the array to see if we keep looping. --->
- <cfset ArrayAppend(
- ) />
Turns out, the loop only iterates 4 times; the attributes only get evaluated that first time. When you stop to think about it, that makes a lot of sense; it is just a string. And if you take Conditional loops into account, it makes sense that the condition="" attribute is a string, not an evaluation. That way it can do a sort of delayed evaluation on the condition.
Man, understanding the basics is so important.
Looking For A New Job?
- ColdFusion Developer at GAP Solutions, Inc.
- Expert Coldfusion Developer with extensive Microsoft Media Services expertise at Atprime Media Services
- Senior CF Backend Developer at Explosive Concepts, Inc.
- AngularJS Opportunities at Top Companies. Get Better Compensation & Culture! at AngularJobs.com
- 7 plus year CF Developer to assist live website at Atprime Media Services
A conditional loop is defined with a condition = "expression" with an expression one expects evaluation per iteration.
In an index loop the docs clearly state for from,to "starting value", and "ending value", to me this clearly means a fixed value or a variable/expression that is evaluated once.
Your test just confirms it.