Consider this structure:
<cfset stFoo = {
a = "one"
, b = "two"
, c = "three"
, d = "foue"
} />
Notice the use of the attribute
item
instead ofindex
.
Attribute | Required | Type | Default | Description |
---|---|---|---|---|
collection | true | structure | A struct object. The variable must be evaluated (wrapped with ##). | |
item | true | string | The current structure key , |
<cfoutput>
<cfloop collection="#stFoo#" item="x">
<li>#structFind(stFoo, x)#</li>
</cfloop>
</cfoutput>
<cfoutput>
<cfloop collection="#stFoo#" item="x">
<li>#stFoo[x]#</li>
</cfloop>
</cfoutput>
This will also have a line break between each line of HTML.
<li>one</li>
<li>two</li>
<li>three</li>
<li>four</li>
With the
FOR IN
syntax,x
is akey
of the structure object.
<cfscript>
for (x in stFoo) {
writeOutput("<li>" & x & "</li>");
}
</cfscript>
<li>A</li><li>B</li><li>C</li><li>D</li>
<cfscript>
for (x in stFoo) {
writeOutput("<li>" & structFind(stFoo, x) & "</li>");
}
</cfscript>
<cfscript>
for (x in stFoo) {
writeOutput("<li>" & stFoo[x] & "</li>");
}
</cfscript>
The cfscript function
cfloop
has no support forcollection
.
Notice that the cfscript output is all on one line.
<li>one</li><li>two</li><li>three</li><li>four</li>