Does ColdFusion have a short syntax for creating a struct?
Solution 1
Coldfusion 8 (and up) has a struct literal notation:
<cfset objData = {
Key1 = "Value1",
Key2 = "Value2"
}>
However, there are a few strings attached:
- bennadel.com: "Learning ColdFusion 8: Implicit Struct And Array Creation"
- barneyb.com: "ColdFusion Struct Literals Fail Again"
- barneyb.com: "CF8 Structure Literal Gotcha"
Note: ColdFusion 9 fixed the errors outlined above, so with any CF version available nowadays you will be fine. I'm still leaving in the links for reference.
Starting with CF10, you can also use the syntax that's familiar from JavaScript:
<cfset objData = {
Key1: "Value1",
Key2: "Value2"
}>
Solution 2
If your attempts to simplify struct syntax in CF8 run into nesting and/or inline deficiencies you can use this deceptively simple function:
<cfscript>
function nStruct(){
return arguments;
}
</cfscript>
You can then use this syntax:
<cfdump var="#nStruct(
a=1,
b=nStruct(
c=2,d=3
)
)#" />
Solution 3
In ColdFusion 8 and above you can create a structure like this:
ref={template="label", language="en"}
Solution 4
You could use cfjson. Add the component to a scope you're using (e.g. the request scope):
<cfobject name="request.json" component="cfc.json">
and call it like:
<cfset aStructure = request.json.decode('{ Template: "Label", Language: "en" }')>
Related videos on Youtube
Kip
I've been programming since I got my hands on a TI-83 in precalculus class during junior year of high school. Some cool stuff I've done: Chord-o-matic Chord Player: find out what those crazy chords are named! Everytime: keep track of the current time in lots of time zones from your system tray BigFraction: open source Java library for handling fractions to arbitrary precision. JSON Formatter: a completely client-side JSON beautifier/uglifier. QuickReplace: a completely client-side regex tool. It's behind some ugly developer UI since I created it for myself to use. (Sorry not sorry.)
Updated on November 12, 2021Comments
-
Kip over 2 years
Is there any "short" syntax for creating a struct in ColdFusion? I'd like to replace this verbose code:
<cfscript> ref = StructNew(); ref.Template = "Label"; ref.Language = "en"; stcML = GetPrompts(ref); </cfscript>
with something more like a JavaScript object:
<cfscript> stcML = GetPrompts({ Template: "Label", Language: "en" }); </cfscript>
Is there anything like this?
-
marc esher over 14 yearsKip, in CF8 you can use this syntax, as Sam says, but you can't use it in arguments to functions. In CF9, you can use the struct shorthand notation in arguments to functions; thus, your example above would work (with = instead of : though)
-
Kip over 14 yearsso basically you can only use that short syntax for creating a 1-dimensional struct that is assigned to a variable. but you can't create one to pass to a function on the fly like i wanted. I guess that's more like a few chains attached...
-
Kip over 14 yearssounds like this will be fixed in CF9 though!
-
Tomalak over 14 yearsYeah, "chains" may be more accurate. I'd file CF8 struct literals under "one day late and one dollar short". Not sure why they rolled that out - in it's current form, it does not make much sense.
-
Adam Tuttle over 14 yearsIn ColdFusion 9, implicit structure and array notation is fixed. Nesting works, and you can create them "on the fly" as function or tag arguments: doFoo({foo="bar"}) or <cffile attributeCollection="#{action='read', ...}#"/>
-
Doug Cassidy over 7 yearsI named mine 'array()' cuz that's how it works in php ;)
-
jinglesthula almost 6 yearsIt should also be noted that by now (CF 10 maybe?) they've added support for the more obvious
:
so you don't have to use the assignment operator (=
). So yes, you may now (both in CFML and cfscript) use exactly what you were hoping for.