Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does ColdFusion have a short syntax for creating a struct?

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?

like image 414
Kip Avatar asked Aug 19 '09 16:08

Kip


People also ask

How do I create a structure in ColdFusion?

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:

What is ColdFusion used for?

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.

What is a key in ColdFusion?

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.

What are complex variables in ColdFusion?

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.


1 Answers

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"
}>
like image 76
Tomalak Avatar answered Sep 19 '22 17:09

Tomalak