CFML Basics for ColdFusion MX
Learn classic tag-oriented CFML without accidentally depending on syntax introduced after the MX generation.
Templates and Evaluation
A .cfm template can contain HTML, CFML tags, and expressions. Text outside CFML tags is normally returned as output. Expressions inside cfoutput are delimited with hash marks.
<cfset userName = "Travis">
<cfoutput>
<p>Hello, #HTMLEditFormat(userName)#.</p>
</cfoutput>
Comments
<!--- This comment is processed by ColdFusion and not sent to the browser. ---> <!-- This HTML comment is sent to the browser. -->
Variables and Explicit Scopes
ColdFusion can search several scopes when a variable is unqualified. In legacy code that behavior can hide errors. Prefer explicit scope names.
<cfset variables.pageTitle = "Customer List">
<cfparam name="url.page" default="1">
<cfparam name="form.search" default="">
<cfoutput>
Page: #Val(url.page)#
</cfoutput>
Strings, Numbers, Booleans, and Dates
<cfset customerName = "Acme Hosting">
<cfset openTickets = 3>
<cfset isActive = true>
<cfset createdAt = CreateDateTime(2005, 8, 15, 9, 30, 0)>
<cfoutput>
#HTMLEditFormat(customerName)# has #openTickets# open tickets.<br>
Created: #DateFormat(createdAt, "mm/dd/yyyy")#
</cfoutput>
Arrays in MX-Compatible Syntax
Implicit array literals were added after the MX generation. Build arrays with functions when targeting MX.
<cfset topics = ArrayNew(1)>
<cfset ArrayAppend(topics, "ColdFusion")>
<cfset ArrayAppend(topics, "PHP")>
<cfset ArrayAppend(topics, "Networking")>
<cfloop index="i" from="1" to="#ArrayLen(topics)#">
<cfoutput>#HTMLEditFormat(topics[i])#<br></cfoutput>
</cfloop>
Structures
<cfset customer = StructNew()> <cfset customer.id = 42> <cfset customer.name = "Example Company"> <cfset customer.status = "active"> <cfoutput>#HTMLEditFormat(customer.name)#</cfoutput>
Lists
Comma-delimited lists are common in older CFML applications. They are strings, not arrays.
<cfset roles = "customer,billing,administrator">
<cfif ListFindNoCase(roles, "billing") GT 0>
Billing access is enabled.
</cfif>
<cfloop index="roleName" list="#roles#">
<cfoutput>#HTMLEditFormat(roleName)#<br></cfoutput>
</cfloop>
Conditionals and Comparison Operators
<cfset accountStatus = "active">
<cfif accountStatus EQ "active">
Account is active.
<cfelseif accountStatus EQ "suspended">
Account is suspended.
<cfelse>
Account status is unknown.
</cfif>
Legacy CFML commonly uses word operators such as EQ, NEQ, GT, GTE, LT, LTE, AND, OR, and NOT.
Switch Statements
<cfswitch expression="#url.action#">
<cfcase value="list">
<cfinclude template="list.cfm">
</cfcase>
<cfcase value="edit">
<cfinclude template="edit.cfm">
</cfcase>
<cfdefaultcase>
<cfinclude template="home.cfm">
</cfdefaultcase>
</cfswitch>
User-Defined Functions
<cffunction name="formatTicketStatus" returntype="string" output="false">
<cfargument name="status" type="string" required="true">
<cfswitch expression="#arguments.status#">
<cfcase value="open"><cfreturn "Open"></cfcase>
<cfcase value="closed"><cfreturn "Closed"></cfcase>
<cfdefaultcase><cfreturn "Unknown"></cfdefaultcase>
</cfswitch>
</cffunction>
CFScript in the MX Era
CFScript existed before MX, but its coverage was smaller than modern CFScript. Tag syntax remained common, especially for queries, files, mail, and application configuration.
<cfscript>
greeting = "Hello";
name = "Travis";
WriteOutput(greeting & ", " & HTMLEditFormat(name));
</cfscript>
Output Encoding
MX-era code usually uses HTMLEditFormat() before inserting untrusted text into HTML. Encoding must match the output context; HTML encoding is not sufficient for JavaScript, CSS, SQL, or URLs.