Does ColdFusion have a short syntax for creating a struct?

10,079

Solution 1

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"
}>

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" }')>
Share:
10,079

Related videos on Youtube

Kip
Author by

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, 2021

Comments

  • Kip
    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
    marc esher over 14 years
    Kip, 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
    Kip over 14 years
    so 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
    Kip over 14 years
    sounds like this will be fixed in CF9 though!
  • Tomalak
    Tomalak over 14 years
    Yeah, "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
    Adam Tuttle over 14 years
    In 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
    Doug Cassidy over 7 years
    I named mine 'array()' cuz that's how it works in php ;)
  • jinglesthula
    jinglesthula almost 6 years
    It 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.