Skip to content

View Helpers

cycle()

cycle() — returns string

Available in: controller Category: Miscellaneous Functions

cycle() is a view helper used to loop through a list of values sequentially, returning the next value each time it’s called. This is especially useful for things like alternating row colors in tables or assigning sequential classes in repeated HTML elements.

NameTypeRequiredDefaultDescription
valuesstringyesList of values to cycle through.
namestringnodefaultName to give the cycle. Useful when you use multiple cycles on a page.
<!--- Alternating table row colors --->
<table>
	<thead>
		<tr>
			<th>Name</th>
			<th>Phone</th>
		</tr>
	</thead>
	<tbody>
		<cfoutput query="employees">
			<tr class="#cycle("odd,even")#">
				<td>#employees.name#</td>
				<td>#employees.phone#</td>
			</tr>
		</cfoutput>
	</tbody>
</table>

<!--- Alternating row colors and shrinking emphasis --->
<cfoutput query="employees" group="departmentId">
	<div class="#cycle(values="even,odd", name="row")#">
		<ul>
			<cfoutput>
				rank = cycle(values="president,vice-president,director,manager,specialist,intern", name="position")>
				<li class="#rank#">#categories.categoryName#</li>
				resetCycle("emphasis")>
			</cfoutput>
		</ul>
	</div>
</cfoutput>