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?
In ColdFusion, you can create structures explicitly by using a function, and then populate the structure using assignment statements or functions, or you can create the structure implicitly by using an assignment statement. You can create structures by assigning a variable name to the structure with the StructNew function as follows:
The following example shows this use: ColdFusion allows nested implicit creation of structures, arrays, or structures and arrays. For example, You can use object .property notation on the left side of assignments inside structure notation. For example, You cannot use a dynamic variable when you create a structure implicitly.
The values associated with the key can be any valid ColdFusion value or object. It can be a string or integer, or a complex object such as an array or another structure. Because structures can contain any types of data, they provide a powerful and flexible mechanism for representing complex data.
Complex variables help you get more out of ColdFusion, and they're easier to use than you might think. Brian Kotek explains how arrays and structures put problem-solving power in your development toolbox. ColdFusion Markup Language (CFML) lets you create variables to store data. Variables in CFML fall into two categories: simple and complex.
Coldfusion 8 (and up) has a struct literal notation:
<cfset objData = {
Key1 = "Value1",
Key2 = "Value2"
}>
However, there are a few strings attached:
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"
}>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With