Tags

Loop

Overview:

Use the loop tag to repeat certain parts of your code over and over again.

There are 5 kinds of loops:

  • Index Loop

    Use attributes from="[number]", to="[number]", index="[letter]".
    Loops incrementally until the "to" number is reached.

  • Condition Loop

    Use attribute condition="[logical statement, like an if tag]".
    Loops until the specified condition becomes false.

  • Query Loop

    Use attribute query="[name of query variables]".
    Loops once for each record in the query.

  • List Loop

    Use attributes list="[text]", index="[letter]", delimiters="[special characters]".
    Loop once for each index in a list. By default, the list delimiter is a comma. (A comma-delimited list looks like "apple,banana,pear").

  • Structure Loop

    Use attributes collection="[structure variable]", item="[letter]".
    Loop once for each item in a structure.

 

Example:

<!--- INDEX LOOPS --->

  <!--- loop from one to five --->
	<loop from="1" to="5" index="i">
		The current number is #i#<br>
	</loop>

  <!--- loop from one to... whatever the "my_number" variable says --->
	<var my_number = 10>
	<loop from="1" to="5" index="num">
		The current number is #num#<br>
	</loop>

  <!--- loop through an array --->
	<var my_array = ['apple','banana','pear']>
	<loop from="1" to="#arrayLen(my_array)#" index="x">
		The current fruit is #my_array[x]#<br>
	</loop>



<!--- CONDITION LOOPS --->

  <!--- simple counter loop: count to ten --->
	<var _count = 1>
	<loop condition="_count LTE 10">
		The current number is #_count#<br>
		 <var _count = _count + 1>
	</loop>

  <!--- random loop - use a random number between 1 and 9 --->
	<var _num = 1>
	<loop condition="_num NEQ 7">
		Sorry, that number is #_num#, not 7... <br>
		<var _num = randRange(1,9)>
	</loop>
	<b>Congratulations!  You found the number Seven!</b>



<!--- QUERY LOOPS --->

  <!--- loop once for each text by user sarah --->
	<set j="/text@sarah">
		<!--- save the dataset in a "my_query" variable --->
		<var my_query = set>
	</set>
	<loop query="my_query">
		<!--- output each record in the query --->
		<div>
		  <b>#my_query.title#</b>
		  <p>
		  	#my_query.body#
		  </p>
		</div>
	</loop>



<!--- LIST LOOPS --->

  <!--- loop through a simple list --->
	<loop list="apple,banana,pear" index="i">
		The current fruit is #i#<br>
	</loop>

  <!--- loop once for each word in a sentence...
  	  think of a sentence as a space-delimited list. --->
	<loop list="These are some of my favorite things" index="x" delimiters=" ">
		#x#<br>
	</loop>



<!--- STRUCTURE LOOPS --->

  <!--- create a simple structure and output its items --->
	<var dog = structNew()>
	  <var dog.name = "Fido">
	  <var dog.fur_color = "Brown">
	  <var dog.num_legs = 4>
	<h3>Here's what's important about dogs:</h3>
	<loop collection="#dog#" item="i">
		#i#
	</loop>

  <!--- use the current item to output each value in the structure --->
	<h3>Here's one particular dog:</h3>
	<loop collection="#dog#" item="i">
		<b>#i#:</b>  #dog[i]#
	</loop>
 

See Also...