Inside Coldfusion MX

CFScript also enables the programmer to simplify the construction of complex data structures. The tag-based structure of ColdFusion can easily become very tedious for this task. Let's say that we want to create a structure of customer billing information; we could do the following:

<cfset customer = StructNew()> <cfset customer.ID = FORM.CUSID> <cfset customer.name = StructNew()> <cfset customer.name.first = FORM.FirstName> <cfset FSET customer.name.last = FORM.LastName> <cfset customer.billinginfo = StructNew()> <cfset customer.billinginfo.address = StructNew()> <cfset customer.billinginfo.address.street = FORM.street> <cfset customer.billinginfo.address.city = FORM.city> <cfset customer.billinginfo.address.state = FORM.state> <cfset customer.billinginfo.address.zipcode = FORM.zipcode>

In CFScript, we can avoid the use of the CFSET tag and just assign the values to the variable:

<cfscript> customer = StructNew(); customer.ID = FORM.cusid; customer.name = StructNew(); customer.name.first = FORM.firstname; customer.name.last = FORM.lastname; customer.billinginfo = StructNew(); customer.billinginfo.address = StructNew(); customer.billinginfo.address.street = FORM.street; customer.billinginfo.address.city = FORM.city; customer.billinginfo.address.state = FORM.state; customer.billinginfo.address.zipcode = FORM.zipcode; <cfscript>

The construction of arrays is very similar:

<cfset array = ArrayNew(1)> <cfset array[1] = "Bob"> <cfset array[2] = "Alice"> <cfset array[3] = "John"> <cfset array[4] = "Lisa">

In CFScript, it looks like the following:

<cfscript> array = ArrayNew(1); array[1] = "Bob"; array[2] = "Alice"; array[3] = "John"; array[4] = "Lisa"; </cfscript>

Not only is this clearer, but it also saves some typing!

Категории