coldfusion CFLOOP How-To Structure

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

Consider this structure:

<cfset stFoo = {
    a = "one"
    , b = "two"
    , c = "three"
    , d = "foue"
} />

Tag syntax

Parameters

Notice the use of the attribute item instead of index.

AttributeRequiredTypeDefaultDescription
collectiontruestructureA struct object. The variable must be evaluated (wrapped with ##).
itemtruestringThe current structure key,

Using Structure Functions

<cfoutput>
    <cfloop collection="#stFoo#" item="x">
        <li>#structFind(stFoo, x)#</li>
    </cfloop>
</cfoutput>

Implicit Structure Syntax

<cfoutput>
    <cfloop collection="#stFoo#" item="x">
        <li>#stFoo[x]#</li>
    </cfloop>
</cfoutput>

Generated HTML

This will also have a line break between each line of HTML.

<li>one</li>
<li>two</li>
<li>three</li>
<li>four</li>

CFScript

With the FOR IN syntax, x is a key of the structure object.

Output the structure's keys

<cfscript>
    for (x in stFoo) {
        writeOutput("<li>" & x & "</li>");
    }
</cfscript>

Generated HTML

<li>A</li><li>B</li><li>C</li><li>D</li>

Output the value of the structure's keys

Using Structure Functions

<cfscript>
    for (x in stFoo) {
        writeOutput("<li>" & structFind(stFoo, x) & "</li>");
    }
</cfscript>

Implicit Structure Syntax

<cfscript>
    for (x in stFoo) {
        writeOutput("<li>" & stFoo[x] & "</li>");
    }
</cfscript>

ColdFusion 11 through current

The cfscript function cfloop has no support for collection.

Generated HTML

Notice that the cfscript output is all on one line.

<li>one</li><li>two</li><li>three</li><li>four</li>


Got any coldfusion Question?