Using The Iif Function - MACROMEDIA COLDFUSION MX 61-DEVELOPING COLDFUSION MX Develop Manual

Developing coldfusion mx applications
Table of Contents

Advertisement

In the second line, enclosing the myVar#i# variable name in quotation marks tells ColdFusion to
evaluate the name and process any text in pound signs as a variable or function. ColdFusion
replaces the #i# with the value of the variable i, so that if the value of i is 12, this code is
equivalent to the line
<cfset myVar12 = myVal>
For more information on this usage, see
assignments" on page

Using the IIF function

The
function is a shorthand for the following code:
IIf
<cfif argument1>
<cfset result = Evaluate(argument1)>
<cfelse>
<cfset result = Evaluate(argument2)>
</cfif>
The function returns the value of the result variable. It is comparable to the use of the JavaScript
and Java ? : operator, and can result in more compact code. As a result, the
convenient even if you are not using dynamic expressions.
The
function requires the
IIF
as the following example shows:
<cfoutput>
#IIf(IsDefined("LocalVar"), "LocalVar", DE("The variable is not
defined."))#
</cfoutput>
If you do not enclose the string "The variable is not defined." in a
tries to evaluate the contents of the string as an expression and generates an error (in this case, an
invalid parser construct error).
The
function is useful for incorporating ColdFusion logic in-line in HTML code, but it
IIF
entails a processing time penalty in cases where you do not otherwise need dynamic expression
evaluation.
The following example shows using
and gray. It also shows the use of the
strings.
<cfoutput>
<table border="1" cellpadding="3">
<cfloop index="i" from="1" to="10">
<tr bgcolor="#IIF( i mod 2 eq 0, DE("white"), DE("gray") )#">
<td>
hello #i#
</td>
</tr>
</cfloop>
</table>
</cfoutput>
This code is more compact than the following example which does not use
<cfoutput>
<table border="1" cellpadding="3">
<cfloop index="i" from="1" to="10">
98
Chapter 4: Using Expressions and Pound Signs
"Using pound signs to construct a variable name in
93.
function to prevent ColdFusion from evaluating literal strings,
DE
to alternate table row background color between white
IIF
function to prevent ColdFusion from evaluating the color
DE
function can be
IIF
function, the
function
DE
IIF
or
.
IIF
DE

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the COLDFUSION MX 61-DEVELOPING COLDFUSION MX and is the answer not in the manual?

Questions and answers

This manual is also suitable for:

Coldfusion mx

Table of Contents