Requirements

 
Prerequisite knowledge
Although an existing knowledge of ColdFusion would help, this article does not require you to be an expert in ColdFusion. A general understanding of ColdFusion tags and other core language syntax is enough to get most out of this article.
 
User level
All
 
 
Required products
Adobe ColdFusion Enterprise Edition (2016 release) (Download trial)
 
 
Sample files

 
Introduction

Adobe ColdFusion 11, introduces many new language features to make the ColdFusion development a lot easier and more productive.
 
Language enhancement is one area that always bring excitement among ColdFusion developers. As always, many new features are added to ColdFusion 11. Oject-oriented programming (OOPs) syntaxes, as member function and script supports for CF tags are added to bring OOPs coding style to ColdFusion. Also, we have added and enhanced the existing language constructs to make ColdFusion more powerful.
 

 
Features list of Language Enhancements in ColdFusion 11

The complete language feature enhancements in ColdFusion 11 can be grouped under six categories.
 
Here is the list of language features of ColdFusion 11:
 
  1. Script support for ColdFusion tags
  2. Member Functions for ColdFusion data type
  3. Improved JSON serialization
  4. Easy to use CF functions for Query tags
  5. Elvis operator (?:)
  6. Promoting built-in ColdFusion functions to first class
Let’s discuss each and every features, at a time, to know more on its implementation, syntax and usage.
 

 
Script support for ColdFusion tags

From ColdFusion 11, you can now use all the relevant ColdFusion tags within CFScript blocks. Prior to ColdFusion 11, barring few, the ColdFusion tags were not available in the script syntax. If you are coding in script block and required to call a tag, you had no choice but to come out of script block and use tag syntax. With script support for CF tags added to ColdFusion 11, you can now enjoy writing all your code in a CFSCRIPT block itself.
 
Script support is added for both simple tags as well as nested tags. For instance, you can use a simple CFHTTP tag in a CFScript block (refer /script/http_simple.cfm), as follows:
 
<cfscript> cfhttp(url=”www.google.com”, method=”GET”); </cfscript>
Or you can invoke a CFHTTP tag passing additional information through CFHTTPPARAM, making it a nested call, as shown below:
 
<cfscript> Cfhttp(URL=http://#CGI.SERVER_NAME#.../target.cfm, method=”GET”) { cfhttpparam(type="url", name='emp_name' value=”Awdhesh”); cfhttpparam(type="header", name='myheader' value=”My custom header”); } Writeoutput(cfhttp.filecontent); <cfscript>
You can refer attached file script/http_nested.cfm.
 
As general syntax for script support, a ColdFusion tag is invoked like a function call in CFSCRIPT block with tag name is used for the function name. The tag attributes are passed as comma separated name-value pairs to it, like arguments are passed to a function. The child tag (and the body in general) is defined within a curly brackets, just like a function block.
 
 
Script support for Custom tags
The script syntax has also been extended to invoke custom tags. There are two ways to invoke a custom tags in ColdFusion and both of them are supported for CFSCRIPT block. These two ways are:
 
  1. Cfm file based custom tags
  2. Prefix based custom tags  
Let’s have a look at first option. A custom tag “happybirthday.cfm” is being called from “custom1.cfm”, as follow:
 
<cfscript> // cfm file based custom tags Cf_happybirthday(name=”john”, birthdate=”December 5, 1987”); </cfscript>
The second option is for to invoke prefix based custom tag invocation. You first import a taglib and define a prefix to use custom tags under your taglib folder. A custom tag file “validateUser.cfm” is present under “securityTags” folder. This custom tag is used with prefix “security”, as follow:
 
<cfscript> // Prefix based custom tags cfimport(taglib="securityTags", prefix="security"); security:validateUser(name=’John’); </cfscript>

 
Member Functions for CF data type

Member functions syntax, added in CF 11, brings object-oriented programming (OOPs) paradigm to ColdFusion 11. To manipulate a CF object, e.g an array or a struct, you had the only option to call headless function  like ArrayAppend or StructFind. There was no way you can call a member function on the given ColdFusion data objects, like Java. With support for “member function” added in ColdFusion 11, you can now code in true object-oriented style while performing an action on CF datatype. Let’s see both old and new coding styles to retrieve an entry from a  struct:
 
Headless function (old CF10 style)
 
<cfset StructFind(empStruct,’id’) >
New OOPs based syntax
 
<cfset employstruct.find(‘id’) >
The member functions support in ColdFusion is pretty exhaustive and cover most of the data types present in ColdFusion. The data types, for which member functions supported, are:
 
  1. String
  2. List
  3. Array
  4. Struct
  5. Date
  6. Image
  7. Query
  8. SpreadSheet
  9. XML
As it is not possible to cover the complete details of the member functions support here, I recommend you to refer the ColdFusion 11 documentation to know more on it.
 

 
Improved JSON Serialization

JSON serialization helps you to convert ColdFusion data into JSON representation. It was first introduced in ColdFusion 8 and is improved over the subsequent versions, including ColdFusion 11.
 
The complete list of JSON-level enhancement added to ColdFusion 11 are:
 
  1. Data type preservation for Query and CFC objects
  2. Case preservation of Struct keys
  3. Added “struct” as new QueryFormat
  4. Custom serializer
 
Data type preservation for Query and CFC objects
The earlier approach of parsing and identifing a CFC property or a query column type wasn’t foolproof. At times, a property value (eg: zip code value as “01003”), meant to be serialized as string, was forced converted to number while serializing. Similarly, sometime ColdFusion improperly serializes a string object as boolean or Date data type. Now ColdFusion 11 onwards, this forced conversion of string data type will be stopped. The right data type, to which a string object is to be serialized will be decided based on information retrieved from the underlying metadata. For the query object, the metadata will be fetched from the datasource and for the CFC it would be build from the type attributes of CFC object’s property fields. For instance, you can now serialize a CFC with zip code properly, as follow:
 
zipcode.cfc
 
<cfcomponent accessors="true" ... > <cfproperty name='zipcode' type="string" /> </cfcomponent>
cfc_datatype.cfm
 
<cfscript> mycfc = createObject("component", "Entity"); mycfc.setZipCode("01003"); writeoutput(serializeJSON(mycfc)); </cfscript>
The output of above code will be:
 
 ColdFusion 10: {“zipcode” : 1003}
 
 ColdFusion 11: {“zipcode” : “01003”}
 
Similarly, if you try serializing a query, the data type information will be retrieved from underlying datasource and will be used to serialize it to right datatype.
 
 
Case preservation of the struct keys
ColdFusion 11 preserve the case of a struct key. Previously, the case of a struct key always used to get converted to uppercase. Let’s look at the following example:
 
<cfscript> data = Structnew(); data.empName = “John”; writeoutput(serializeJSON(data)); </cfscript>
The output of the above code in ColdFusion 10 and 11:
 
 ColdFusion 10: {‘EMPNAME’: ‘John’}
 
 ColdFusion 11: {‘empName’: ‘John’}
 
ColdFusion 11 provides you a flag to switch back to old behavior. There are two places where you can set this flag. The first one is in the ColdFusion Administrator and the second one is setting it in Application.cfc file.
 
 
Added “Struct” as new QueryFormat
ColdFusion 11 provides an additional QueryFormat type to serialize a query object to. Row and column were the two supported QueryFormat types. However, none of these two formats are fit for jQuery type JavaScript frameworks. These JavaScript frameworks work well with column-value (like struct) format. With the new “struct” format being added, the integration with these framework will get lot easier in ColdFusion. Now, three different format types row, column, and struct are supported to serialize a query object to JSON representation.
 
 Let’s try to serialize a query object of 2 rows and 2 columns (color, id) and see how it gets serialized with column and struct QueryFormat types.
 
Column format: {“ROWCOUNT”: 2, “COLUMNS”: [“COLOR”, “ID”], DATA: {“ COLOR”: [“red, blue”], “ID”: [1,2]}}
 
Struct format: [{“COLOR”: “red”, “ID”: 1}, {“ COLOR”: “blue”, “ID”: 2}]
 
As shown above, the struct is key-value based format and quite simple. The downside  of this format is the repetition of the column names. It is fine with small resultset, as shown in example, but might get tricky with higher row counts.
 
 
Custom Serializer
ColdFusion 11 provides you an option to hook your own custom serializer for serializing a ColdFusion object. The custom serializer allow you to register your own handler for serializing and deserializing the complex object. If the serializer is not specified, ColdFusion will use the in-built serialization handler.
 
It is two steps process. First you implement the custom serializer and then register it by adding its entry to application.cfc. 
 
I recommend you to refer the ColdFusion 11 documentation to know more on how to implement and register a custom serializer.
 

 
Easy to use ColdFusion functions for Query tags

ColdFusion 11 provides an easy to use ColdFusion functions to execute a query in CFSCRIPT block. The CFQuery tag is available in script through generic “Script support for tags” as well. However, this generic solution might get tricky and make your code look clustered and tiddy if the sql statement is bit comlex and requires too many query param to be passed. To simplify its implemenation in script block, the query functions have been added in ColdFusion 11. The general syntax of query functions are:
 
  QueryExecute(sql_statement);
 
  QueryExecute(sql_statement, queryParams);  
 
  QueryExecute(sql_statement, queryParams, queryOptions);  
 
  sql_statement: is the SQL command to be executed. Like,   “select * from employee”.
 
  queryParams: The query parameters required to execute a dynamic query. It can be  a struct (for named parameter) or an array (for positional parameter).
 
  queryOptions: It is a struct which contains different attributes to a query tag.
 
Let’s look at the following code snippet to know how a queryparams and a queryoptions are passed to QueryExecute function call. You can also refer the sample files query_function/test.cfm.
 
// Named query parameters
 
QueryExecute(“select * from Artists where state=:state and artistid=:id”, {state=’CA’, id=2});
 
// Positional query parameters.
 
QueryExecute(“select * from Artists where state=? and artistid=?”, [“CA”, 2]);
 
//queryOption: Database info is passed as queryoption
 
QueryExecute(“select * from Artists where artistid=?”, [1], {datasource=’cfartgallery’});
 

 
Elvis Operator (?:)

The Elvis operator is a small but elegant feature added in ColdFusion 11. It helps you to shorten your conditional code and makes it look simpler. Let’s have a look at the following code:
 
   displayName = username ?: ‘anonymous’;
 
Here, if the username variable is defined, then username will be assigned to the displayName otherwise the default value “anonymous” will be assigned to it.
 
Other than variable isdefined check (as shown above), the Elvis operator support has also been provided for function call and expression.
 
securityNumber = securityStruct[‘Joe’] ?: -1; // Retrieving from a struct colourCode = colourArray[index] ?: "black"; // Retrieving from an array employeeName = getEmployeeName(ID) ?: “Joe”; // A function call

 
Promoting built-in ColdFusion functions to first class

ColdFusion 11 allows all built-in functions to be treated as first class object. A first class object is the one which could be passed as an argument to a function call, assigned to a variable or returned as a result of a function invocation. Now, you will be able to treat ColdFusion functions (structInsert, arrayLen, listFind) as objects and hence you can.
 
  1. Pass them as arguments to a function call
  2. Assign them to variables
  3. Return them as a result of a function invocation
Now let’s see how lcase, a built-in ColdFusion function, is passed as an argument to convertArrayForArray UDF function.
 
<cfscript> function convertCaseForArray(Array array, function convertor) { for (var i=1; i <= arrayLen(array); i++){ array[i] = convertor(array[i]); } return array; } // lcase built-in function is being passed as callback. resultantArray = convertCaseForArray(['One', 'Two','Three'], lcase); writedump(resultantArray); </cfscript>
 
Running this code on ColdFusion 10 or earlier version causes the error shown in figure 1 to occur.
 
error1
 
Figure 1
 

 
Where to go from here

This article provides a quick introduction to the prominent language features added in ColdFusion 11. There are many other small features added to ColdFusion 11 but are not included as it was not possible to cover those in this article. QueryGetRow, ListEach, ListMap StructMap are few of those ColdFusion functions which are added to ColdFusion 11 but not covered here. Even for those mentioned in this article, it was not possible to cover them in depth here, I recommend you referring the online ColdFusion 11 document for more information on individual feature. You can also read various blog posts on the Adobe ColdFusion blogsite to get more information on language features.
 
Share your feedback about this page or the Adobe Developer Connection in general by using Adobe forums