-
COLDFUSION MX 7 ® ColdFusion MX Developer’s Guide
-
Trademarks 1 Step RoboPDF, ActiveEdit, ActiveTest, Authorware, Blue Sky Software, Blue Sky, Breeze, Breezo, Captivate, Central, ColdFusion, Contribute, Database Explorer, Director, Dreamweaver, Fireworks, Flash, FlashCast, FlashHelp, Flash Lite, FlashPaper, Flex, Flex Builder, Fontographer, FreeHand, Generator, HomeSite, JRun, MacRecorder, Macromedia, MXML, RoboEngine, RoboHelp, RoboInfo, RoboPDF, Roundtrip, Roundtrip HTML, Shockwave, SoundEdit, Studio MX, UltraDev, and WebHelp are either registered tradema
-
CONTENTS INTRODUCTION: About ColdFusion MX Documentation . . . . . . . . . . . . . . . . . . 13 Using this manual . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 CHAPTER 1: Introducing ColdFusion MX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 About Internet applications and web application servers . . . . . . . . . . . . . . . . . . . . 19 About ColdFusion MX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
-
About scopes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Ensuring variable existence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Validating data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Passing variables to custom tags and UDFs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 CHAPTER 4: Using Expressions and Number Signs .
-
CHAPTER 9: Writing and Calling User-Defined Functions. . . . . . . . . . . . . . . . . 171 About user-defined functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 Creating user-defined functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Calling user-defined functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 Working with arguments and variables in functions . . . . . . . . . . . . . . . . . .
-
CHAPTER 14: Handling Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 About error handling in ColdFusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 Understanding errors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 Error messages and the standard error format . . . . . . . . . . . . . . . . . . . . . . . . . . . 314 Determining error-handling strategies . . . . . . . . . . . . . . . . . . .
-
PART IV: Accessing and Using Data CHAPTER 19: Introduction to Databases and SQL . . . . . . . . . . . . . . . . . . . . . . 447 What is a database? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447 Using SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452 Writing queries using an editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
-
CHAPTER 25: Using Verity Search Expressions . . . . . . . . . . . . . . . . . . . . . . . . 581 About Verity query types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581 Using simple queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582 Using explicit queries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583 Using natural queries. . . . . . . . . . . . . . . . . . . . . .
-
CHAPTER 30: Creating Skinnable XML Forms . . . . . . . . . . . . . . . . . . . . . . . . . 709 About XML skinnable forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Building XML skinnable forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ColdFusion XML format. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating XSLT skins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
-
PART VI: Using Web Elements and External Objects CHAPTER 35: Using XML and WDDX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843 About XML and ColdFusion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843 The XML document object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844 ColdFusion XML tag and functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 850 Using an XML object . . . . . . . . . .
-
PART VII: Using External Resources CHAPTER 39: Sending and Receiving E-Mail . . . . . . . . . . . . . . . . . . . . . . . . . . 973 Using ColdFusion with mail servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sending e-mail messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sample uses of the cfmail tag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using the cfmailparam tag. . . . . . . . . . . . . . .
-
CHAPTER 44: Using the SMS Event Gateway . . . . . . . . . . . . . . . . . . . . . . . . 1065 About SMS and ColdFusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1065 Configuring an SMS event gateway. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1071 Handling incoming messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1073 Sending outgoing messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
-
INTRODUCTION About ColdFusion MX Documentation ColdFusion MX Developer’s Guide provides the tools needed to develop Internet applications using Macromedia ColdFusion MX. This manual is intended for web application programmers who are learning ColdFusion MX or wish to extended their ColdFusion MX programming knowledge. It provides a solid grounding in the tools that ColdFusion MX provides to develop web applications.
-
Beginning with ColdFusion If you are learning ColdFusion, it might be most effective to read this manual in the following order: 1. Chapter 1, “Introducing ColdFusion MX” through Chapter 4, “Using Expressions and Number Signs” to learn the basics of CFML. 2. Chapter 19, “Introduction to Databases and SQL” through Chapter 21, “Updating Your Database” to learn about using databases. 3.
-
• • • • Chapter 42, “Using Event Gateways” Chapter 43, “Using the Instant Messaging Event Gateways” Chapter 44, “Using the SMS Event Gateway” Chapter 45, “Creating Custom Event Gateways” Nearly all chapters contain information that is new in ColdFusion MX 7, so you should also review all other chapters for useful information. The index and the table of contents are useful tools for finding new features or changed documentation. Note: If you are moving from ColdFusion 4.
-
Feature Description Chapters ColdFusion application structure 13–17 ColdFusion supports many ways of building an application, and includes specific features, such as the Application.cfc file or Application.cfm page, built-in security features, and shared scopes, that help you optimize your application structure. Error handling mechanisms ColdFusion provides several mechanisms for handling data, including custom error pages and exception-handling tags and functions, such as cftry and cfcatch.
-
Feature Description Chapters XML skinnable forms ColdFusion can convert your CFML forms into XML and format the 30 XML using XSLT skins and style sheets. Data graphing You can use the cfchart tag to display your data graphically. 31 Reports and printable output You can create output that is formatted for print as PDF or FlashPaper documents. You can also use ColdFusion reporting to create banded reports for display or printing.
-
Feature Description Chapters SMS event gateway 44 Your ColdFusion MX application can communicate with short message service (SMS) devices, such as mobile phones, using the ColdFusion SMS event gateway. Creating gateways You can write your own event gateways in Java and integrate them 45 into ColdFusion MX. Accessing the ColdFusion MX documentation The ColdFusion MX documentation is designed to provide support for the complete spectrum of participants.
-
CHAPTER 1 Introducing ColdFusion MX This chapter describes Macromedia ColdFusion MX and the role it plays in developing dynamic Internet applications. This chapter also introduces the topics discussed in this manual. Contents About Internet applications and web application servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 About ColdFusion MX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
-
ColdFusion MX is a rapid application development environment that lets you build dynamic websites and Internet applications quickly and easily. It lets you develop sophisticated websites and Internet applications without knowing the details of many complex technologies, yet it lets advanced developers take advantage of the full capabilities of many of the latest Internet technologies. About web application servers To understand ColdFusion, you must first understand the role of web application servers.
-
How a web server and web application server work together The following steps explain how a web server and web application server work together to process a page request: 1. The user requests a page by typing a URL in a browser, and the web server receives the request. 2. The web server looks at the file extension to determine whether a web application server must process the page.
-
About ColdFusion MX ColdFusion MX is a rapid scripting environment server for creating dynamic Internet Applications. ColdFusion Markup Language (CFML) is an easy-to-learn tag-based scripting language, with connectivity to enterprise data and powerful built-in search and charting capabilities. ColdFusion MX enables developers to easily build and deploy dynamic websites, content publishing systems, self-service applications, commerce sites, and more.
-
CFML includes approximately 110 tags. ColdFusion tags serve many functions. They provide programming constructs, such as conditional processing and loop structures. They also provide services, such as charting and graphing, full-text search, access to protocols such as FTP, SMTP/ POP, and HTTP, and much more.
-
Verity Search Server The Verity Search Server (also called the Verity search engine) provides full text search capability for documents and data on a ColdFusion MX site. ColdFusion MX Administrator ColdFusion MX Administrator configures and manages the ColdFusion application server. It is a secure web-based application that you can access using any web browser, from any computer with an Internet connection.
-
This part describes the elements of the CFML programming language. It tells you how to use CFML tags, functions, variables and expressions, the CFScript scripting language, and regular expressions. The following chapters are included: Chapter 2: Elements of CFML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Chapter 3: Using ColdFusion Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
-
-
CHAPTER 2 Elements of CFML This chapter provides an overview of the basic elements of CFML, including tags, functions, constants, variables, expressions, and CFScript. The chapters in Part I of this manual describe these topics in detail. Contents CFML Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
-
CFML Basics This chapter introduces and describes the basic elements of CFML. These elements make CFML a powerful tool for developing interactive web applications.
-
Note: You cannot embed comments inside a tag name or function name, such as CustomTag>. You also cannot embed comments inside strings, as in the following example: IsDefined("MyVariable"). Tags ColdFusion tags tell the ColdFusion server that it must process information. The ColdFusion server only processes tag contents; it returns text outside of ColdFusion to the web server unchanged.
-
• Using external tools and objects, including Verity collections, COM, Java, and CORBA objects, and executable programs • Using protocols, such as mail, http, ftp, and pop Much of this document describes how to use these tags effectively. CFML Reference documents each tag in detail. Custom tags ColdFusion lets you create custom tags.
-
Functions Functions typically manipulate data and return a result. CFML includes over 280 built-in functions. You can also create user-defined functions (UDFs), sometimes referred to as custom functions. Functions have the following general form: functionName([argument1[, argument2]]...) Some functions, such as the Now function take no arguments. Other functions require one or more comma-separated arguments and can have additional optional arguments. All ColdFusion functions return a value.
-
User-defined functions You can write your own functions, user-defined functions (UDFs). You can use these functions in ColdFusion expressions or in CFScript. You can call a user-defined function anywhere you can use a built-in CFML function. You create UDFs using the cffunction tag or the CFScript function statement. UDFs that you create using the cffunction tag can include ColdFusion tags and functions. UDFs that you create in CFScript can only include functions.
-
Variables Variables are the most frequently used operands in ColdFusion expressions. Variable values can be set and reset, and can be passed as attributes to CFML tags. Variables can be passed as parameters to functions, and can replace most constants. ColdFusion has a number of built-in variables that provide information about the server and are returned by ColdFusion tags. For a list of the ColdFusion built-in variables, see Chapter 1, “Reserved Words and Variables” in CFML Reference.
-
Scope Description Client Variables that are associated with one client. Client variables let you maintain state as a user moves from page to page in an application and are available across browser sessions. Session Variables that are associated with one client and persist only as long as the client maintains a session. Application Variables that are associated with one, named, application on a server. The Application.
-
Data types ColdFusion is considered typeless because you do not explicitly specify variable data types. However, ColdFusion data, the constants and the data that variables represent, do have data types, which correspond to the ways the data is stored on the computer. ColdFusion data belongs to the following type categories: Category Description and types Simple Represents one value. You can use simple data types directly in ColdFusion expressions.
-
This section provides a basic introduction to using flow-control tags. CFScript also provides a set of flow-control statements. For information on using flow-control statements in CFScript, see Chapter 6, “Extending ColdFusion Pages with CFML Scripting,” on page 123. For more details on using flow-control tags, see the reference pages for these tags in CFML Reference. cfif, cfelseif, and cfelse The cfif, cfelseif, and cfelse tags provide if-then-else conditional processing, as follows: 1.
-
The following example shows switch processing: #FirstName# #LastName# is in Sales
#FirstName# #LastName# is in Accounting
#FirstName# #LastName# is in Administration
#FirstName# #LastName# is not in Sales, Accounting, or Administration.
-
i is #i#
foundit is #foundit#
Note: You can get an infinite conditional loop if you do not force an end condition.
-
Character case ColdFusion is case-insensitive. For example, the following all represent the cfset tag: cfset, CFSET, CFSet, and even cfsEt. However, you should get in the habit of consistently using the same case rules in your programs; for example: • Develop consistent rules for case use, and stick to them. If you use lowercase characters for some tag names, use them for all tag names. • Always use the same case for a variable.
-
• Built-in function names, such as Now or Hash • Scope names, such as Form or Session • Any name starting with cf. However, when you call a CFML custom tag directly, you prefix the custom tag page name with cf_.
-
{ if(Find("key",strings[i],1)) break; } Entry #i# starts with "key"
You use CFScript to create user-defined functions. For more information on CFScript, see Chapter 6, “Extending ColdFusion Pages with CFML Scripting,” on page 123. For more information on user-defined functions, see Chapter 9, “Writing and Calling User-Defined Functions,” on page 171.
-
Chapter 2: Elements of CFML
-
CHAPTER 3 Using ColdFusion Variables Macromedia ColdFusion variables are the most frequently used operands in ColdFusion expressions. Variable values can be set and reset, and can be passed as attributes to CFML tags. Variables can be passed as parameters to functions, and can replace most constants. This chapter describes how to create and use ColdFusion variables.
-
Creating variables You create most ColdFusion variables by assigning them values. (You must use the ArrayNew function to create arrays.) Most commonly, you create variables by using the cfset tag. You can also use the cfparam tag, and assignment statements in CFScript. Tags that create data objects also create variables. For example, the cfquery tag creates a query object variable. ColdFusion automatically creates some variables that provide information about the results of certain tags or operations.
-
Note: In some cases, when you use an existing variable name, you must enclose it with number signs (#) to allow ColdFusion to distinguish it from string or HTML text, and to insert its value, as opposed to its name. For more information, see Chapter 4, “Using number signs,” on page 81.
-
Object type See ColdFusion component Chapter 10, “Building and Using ColdFusion Components,” on page 201 Web service Chapter 36, “Using Web Services,” on page 885 Data type notes Although ColdFusion variables do not have types, it is often convenient to use “variable type” as a shorthand for the type of data that the variable represents. ColdFusion can validate the type of data contained in form fields and query parameters.
-
Integers ColdFusion supports integers between -2,147,483,648 and 2,147,483,647 (32-bit signed integers). You can assign a value outside this range to a variable, but ColdFusion initially stores the number as a string.
-
Escaping quotation marks and number signs To include a single-quotation character in a string that is single-quoted, use two single-quotation marks (known as escaping the single-quotation mark).
-
Boolean values A Boolean value represents whether something is true or false. ColdFusion has two special constants—True and False—to represent these values. For example, the Boolean expression 1 IS 1 evaluates to True. The expression "Monkey" CONTAINS "Money" evaluates to False.
-
Date and time formats You can directly enter a date, time, or date and time, using standard U.S. date formats. ColdFusion processes the two-digit-year values 0 to 29 as twenty-first century dates; it processes the two-digit-year values 30 to 99 as twentieth century dates. Time values can include units down to seconds. The following table lists valid date and time formats: To specify Use these formats Date October 30, 2003 Oct 30, 2003 Oct.
-
How ColdFusion stores dates and times ColdFusion stores and manipulates dates and times as date-time objects. Date-time objects store data on a time line as real numbers. This storage method increases processing efficiency and directly mimics the method used by many popular database systems. In date-time objects, one day is equal to the difference between two successive integers. The time portion of the date-andtime value is stored in the fractional part of the real number.
-
Function Description ToBinary Converts Base64 encoded data to binary data. Macromedia recommends using the BinaryDecode function, instead of this function, in new applications. ToString Converts most simple data types to string data. It can convert numbers, date-time objects, and boolean values. (It converts date-time objects to ODBC timestamp strings.) Macromedia recommends that you use the CharsetEncode function to convert binary data to a string in new applications.
-
You can also create a structure by assigning a value in the structure. For example, the following line creates a new structure called MyStruct with a key named MyValue, equal to 2: Note: In previous ColdFusion versions, this line created a Variables scope variable named "MyStruct.MyValue" with the value 2. After you create a structure, you can use functions or direct references to manipulate its contents, including adding key-value pairs.
-
When you assign a query to a new variable, ColdFusion does not copy the query object. Instead, both names point to the same record set data. For example, the following line creates a new variable, myQuery2, that references the same record set as the myQuery variable: If you make changes to data in myQuery, myQuery2 also shows those changes. You reference query columns by specifying the query name, a period, and the column name; for example: myQuery.
-
Assigning objects to scopes You can give a query or structure a different scope by assigning it to a new variable in the other scope. For example, the following line creates a server variable, Server.SScopeQuery, using the local myquery variable: To clear the server scope query variable, reassign the query object, as follows:
-
Similarly, the following lines print the name "marjorie", the value of the second row in the column: #mycol[2]#
However, when you make an assignment that requires a simple value, ColdFusion automatically converts the query column to the value of the first row. For example, the following lines display the name "ben" twice: #myQuery.
-
4. If a or myVar.a is the name of a complex object, checks whether b is the name of a simple variable, and returns the value of b. If myVar is a complex object but a is not a complex object, checks whether a.b is the name of a simple variable and returns its value. If myVar.a is not a complex object, checks whether myVar.a.b is the name of a simple variable and returns its value. This way, ColdFusion correctly resolves the variable name and can get its value.
-
However, if the name before the first period is either Cookie or Client, ColdFusion uses a different rule. It treats all the text (including any periods) that follow the scope name as the name of a simple variable, because Cookie and Client scope variables must be simple. If you have the following code, you see that ColdFusion creates a single, simple Client scope variable named myVar.a.b:
-
Data type conversion ColdFusion automatically converts between data types to satisfy the requirements of an expression’s operations, including a function’s argument requirements. As a result, you generally don’t need to be concerned about compatibility between data types and the conversions from one data type to another. However, understanding how ColdFusion evaluates data values and converts data between types can help you prevent errors and create code more effectively.
-
Value As Boolean As number As date-time As string False False 0 Error "No" Number True if Number Number is not 0; False otherwise. See “Date-time values” earlier in this chapter. String representation of the number (for example, “8”). String If "Yes", True If "No", False If it can be converted to 0, False If it can be converted to any other number, True If it represents a number (for example, "1,000" or "12.36E-12"), it is converted to the corresponding number.
-
Converting date and time data To ensure that a date and time value is expressed as a real number, add zero to the variable.
-
However, if myVariable has a numeric value such as 12, only the first example produces a result. In the second case, the value of myVariable is not converted to a Boolean data type, because the IS operator does not require a specific data type and just tests the two values for identity. Therefore, ColdFusion compares the value 12 with the constant True. The two are not equal, so nothing is printed.
-
Date-time functions and queries when ODBC is not supported Many CFML functions, including the Now, CreateDate, CreateTime, and CreateDateTime functions, return date-time objects. ColdFusion creates Open Database Connectivity (ODBC) timestamp values when it converts date-time objects to strings. As a result, you might get unexpected results when using dates with a database driver that does not support ODBC escape sequences, or when you use SQL in a query of queries.
-
Using quotation marks To ensure that ColdFusion properly interprets string data, surround strings in single- or doublequotation marks. For example, ColdFusion evaluates “10/2/2001” as a string that can be converted into a date-time object. However, it evaluates 10/2/2001 as a mathematical expression, 5/2001, which evaluates to 0.00249875062469. Examples of type conversion in expression evaluation The following examples demonstrate ColdFusion expression evaluation.
-
Scope types The following table describes ColdFusion scopes: Scope Description Variables (local) The default scope for variables of any type that are created with the cfset and cfparam tags. A local variable is available only on the page on which it is created and any included pages (see also the Caller scope). Form Contains variables passed from a Form page to its action page as the result of submitting the form. (If you use the HTML form tag, you must use method="post".
-
Scope Description Cookie Contains variables maintained in a user’s browser as cookies. Cookies are typically stored in a file on the browser, so they are available across browser sessions and applications. You can create memory-only Cookie variables, which are not available after the user closes the browser. Cookie scope variable names can include periods. Client Contains variables that are associated with one client.
-
Creating and using variables in scopes The following table shows how you create and refer to variables in different scopes in your code. For more information on the mechanisms for creating variables in most scopes, see “Creating variables” on page 44. Scope prefix (type) Prefix required to reference Where available Created by Variables (Local) No On the current page. Cannot be accessed by a form’s action page (unless the form page is also the action page).
-
Scope prefix (type) Prefix required to reference Where available Created by CGI No On any page. Values are specific to the latest browser request. The web server. Contains the server environment variables that result from the browser request. Cffile Yes Following an invocation of cffile. A cffile tag. Cookie No For one client in one or more applications and pages, over multiple browser sessions. A cfcookie tag.
-
Scope prefix (type) Prefix required to reference Where available Created by This Yes Within a ColdFusion component or the body of a user-defined function that was created using the cffunction tag and put in an object, structure, or scope. In the containing page, through the component instance or containing object. Within the component or function by specifying the prefix This when you create the variable.
-
The Java setVariable Response interface method and C++ CCFX::SetVariable method return data to the Variables scope of the calling page. Therefore, they are equivalent to setting a Caller scope variable in a custom ColdFusion tag. Using scopes as structures ColdFusion makes all named scopes available as structures. You cannot access the function-local scope for user defined functions (UDFs) that you define using CFScript as a structure. (In ColdFusion 4.
-
For example, if you submit a form with an unsettled check box, the action page does not get a variable for the check box. The following example from a form action page makes sure the Contractor check box Form variable exists before using it: Contractor: #Form.Contractor# You must always enclose the argument passed to the IsDefined function in double-quotation marks. For more information on the IsDefined function, see CFML Reference.
-
There are two ways to use the cfparam tag to test for variable existence, depending on how you want the validation test to proceed: • With only the name attribute to test that a required variable exists. If it does not exist, the ColdFusion server stops processing the page and displays an error message. • With the name and default attributes to test for the existence of an optional variable. If the variable exists, processing continues and the value is not changed.
-
Validating data It is often not sufficient that input data merely exists; it must also have the right format. For example, a date field must have data in a date format. A salary field must have data in a numeric or currency format. There are many ways to ensure the validity of data, including the following methods: • • • • • Use the cfparam tag with the type attribute to validate a variable. Use the IsValid function to validate a variable.
-
The following example calls a custom tag using two variables, MyString and MyArray: Passing variables to CFX tags You cannot pass arrays, structures, or cfobject objects to CFX tags. You can pass a query to a CFX tag by using the query attribute when calling the tag. ColdFusion normally converts simple data types to strings when passing them to CFX tags; however, the Java Request Interface getIntAttribute method lets you get a passed integer value.
-
CHAPTER 4 Using Expressions and Number Signs This chapter discusses how to use expressions in CFML. It discusses the elements of Macromedia ColdFusion expressions and how to create expressions. It also describes the correct use of number signs to indicate expressions in ColdFusion tags such as cfoutput, in strings, and in expressions. Finally, it describes how to use variables in variable names and strings to create dynamic expressions, and dynamic variables. Contents Expressions . . . . . . . . . . . . .
-
Note that the operator is surrounded by expressions. Each expression can be a simple operand (variable or constant) or a subexpression consisting of more operators and expressions. Complex expressions are built up using subexpressions. For example, in the expression (1 + 1)/2, 1 + 1 is a subexpression consisting of an operator and two operands.
-
Operator Description OR Return True if any of the arguments is True; return False otherwise. For example, True OR False is True, but False OR False is False. XOR Exclusive or: Return True if one of the values is True and the other is False. Return False if both arguments are True or both are False. For example, True XOR True is False, but True XOR False is True. EQV Equivalence: Return True if both operands are True or both are False. The EQV operator is the opposite of the XOR operator.
-
Operator Alternative name(s) LESS THAN LT GREATER THAN OR EQUAL TO GTE, GE LESS THAN OR EQUAL TO LTE, LE Decision operator rules The following rules apply to decision operators: • When ColdFusion evaluates an expression that contains a decision operator other than CONTAINS or DOES NOT CONTAIN, it first determines if the data can be converted to numeric values. If they can be converted, it performs a numeric comparison on the data. If they cannot be converted, it performs a string comparison.
-
Operator precedence and evaluation ordering The order of precedence controls the order in which operators in an expression are evaluated. The order of precedence is as follows: Unary +, Unary ^ *, / \ MOD +, & EQ, NEQ, LT, LTE, GT, GTE, CONTAINS, DOES NOT CONTAIN NOT AND OR XOR EQV IMP To enforce a non-standard order of evaluation, you must parenthesize expressions. For example: • 6 - 3 * 2 is equal to 0 • (6 - 3) * 2 is equal to 6 You can nest parenthesized expressions.
-
All functions return values. In the following example, the cfset tag sets a variable to the value returned by the Now function: You can use the values returned by functions directly to create more complex expressions, as in the following example: Abs(Myvar)/Round(3.14159) For more information on how to insert functions in expressions, see “Using number signs” on page 81.
-
Using number signs Number signs (#) have a special meaning in CFML. When the ColdFusion server encounters number signs in CFML text, such as the text in a cfoutput tag body, it checks to see if the text between the number signs is either a variable or a function. Tip: Number signs are also called pound signs. Is so, it replaces the text and surrounding number signs with the variable value or the result of the function. Otherwise, ColdFusion generates an error.
-
Note: You do not need to use number signs when you use the cfset tag to assign one variable’s value to another value. For example, the following tag assigns the value of the oldVar variable to the new variable, newVar: . Using number signs in tag bodies You can put variables or functions freely inside the bodies of the following tags by enclosing each variable or expression with number signs: • • • cfoutput cfquery cfmail For example: Value is #Form.
-
If number signs are omitted inside the string, the text, rather than the value, appears in the string.
-
In contrast, the following example uses number signs unnecessarily and is less efficient than the previous statement: Dynamic expressions and dynamic variables This section discusses the advanced topics of dynamic expressions, dynamic evaluation, and dynamic variable naming. Many ColdFusion programmers never encounter or need to use dynamic expressions.
-
This process enables ColdFusion to interpret dynamic expressions with variable parts. However, it incurs a substantial processing overhead. Dynamic expressions were important in early versions of ColdFusion, before it supported arrays and structures, and they still can be useful in limited circumstances. However, the ability to use structures and the ability to use associative array notation to access structure elements provide more efficient and easier methods for dynamically managing data.
-
To construct a variable name this way, all the text on the left side of the equal sign must be in quotation marks. This usage is less efficient than using arrays.
-
Using dynamic evaluation The following sections describe how to use dynamic evaluation and create dynamic expressions. ColdFusion dynamic evaluation functions The following table describes the functions that perform dynamic evaluation and are useful in evaluating dynamic expressions: Function Purpose DE Escapes any double-quotation marks in the argument and wraps the result in double-quotation marks.
-
About the Evaluate function The Evaluate function takes one or more string expressions, dynamically evaluates their contents as expressions from left to right, and returns the result of evaluating the rightmost argument.
-
As you can see, using dynamic expressions can result in substantial expression evaluation overhead, and the code can be confusing. Therefore, you should avoid using dynamic expressions wherever a simpler technique, such as using indexed arrays or structures can serve your purposes. Avoiding the Evaluate function Using the Evaluate function increases processing overhead, and in most cases it is not necessary.
-
The following example has the same result as the preceding example and is more efficient: ProductName: #Form["product_" & i]# In this code, ColdFusion does the following: 1. Evaluates the expression in the associative array index brackets as the string "product_" concatenated with the value of the variable i. 2. Determines the value of the variable i; 1. 3. Concatenates the string and the variable value to get product_1. 4.
-
The IIF function requires the DE function to prevent ColdFusion from evaluating literal strings, as the following example shows: #IIf(IsDefined("LocalVar"), "LocalVar", DE("The variable is not defined."))# If you do not enclose the string "The variable is not defined." in a DE function, the IIF function tries to evaluate the contents of the string as an expression and generates an error (in this case, an invalid parser construct error).
-
Example: a dynamic shopping cart The following example dynamically creates and manipulates variable names without using dynamic expression evaluation by using associative array notation. You need to dynamically generate variable names in applications such as shopping carts, where the required output is dynamically generated and variable. In a shopping cart, you do not know in advance the number of cart entries or their contents.
-
| | | |