Used with CFELSE and CFELSEIF, CFIF lets you create simple and compound conditional statements in CFML. The value in the CFIF tag can be any expression.
<CFIF expression> HTML and CFML tags <CFELSEIF> HTML and CFML tags <CFELSE expression> HTML and CFML tags </CFIF>
Note that when testing for the return value of any function that returns a Boolean, you do not need to explicitly define the TRUE condition. The following code uses IsArray as an example:
<CFIF IsArray(myarray)>
When successful, IsArray
evaluates to YES, the string equivalent of the Boolean TRUE. This method is preferred over explicitly defining the TRUE condition:
<CFIF IsArray(myarray) IS TRUE>
Note | On UNIX, there is a switch that provides fast date-time parsing. If you have enabled this switch, you must refer to dates in expressions in the following order: month, day, and year. For example: |
<CFIF "11/23/1998 " GT "11/15/1998 ">
This switch is set on the ColdFusion Administrator Server Settings page. Please refer to Administering ColdFusion Server for more information about ColdFusion settings.
<!--- This example shows the interaction of CFIF, CFELSE, and CFELSEIF ---> ... <H3>CFIF Example</H3> <P>CFIF gives us the ability to perform conditional logic based on a condition or set of conditions. <P>For example, we can output the list of Centers from the snippets datasource by group and only display them <B>IF</B> the city = San Diego. <hr> <!--- use CFIF to test a condition when outputting a query ---> <P>The following are centers in San Diego: <CFOUTPUT QUERY="getCenters" > <CFIF city is "San Diego"> <BR><B>Name/Address:</B>#Name#, #Address1#, #City#, #State# <BR><B>Contact:</B> #Contact#<BR> </CFIF> </CFOUTPUT> <P>If we would like more than one condition to be the case, we can ask for a list of the centers in San Diego <B>OR</B> Santa Ana. If the center does not follow this condition, we can use CFELSE to show only the names and cities of the other centers. <P>Notice how a nested CFIF is used to specify the location of the featured site (Santa Ana or San Diego). <!--- use CFIF to specify a conditional choice for multiple options; also note the nested CFIF ---> <hr> <P>Complete information is shown for centers in San Diego or Santa Ana. All other centers are listed in italics: <CFOUTPUT QUERY="getCenters"> <CFIF city is "San Diego" OR city is "Santa Ana"> <H4>Featured Center in <CFIF city is "San Diego">San Diego<CFELSE>Santa Ana</CFIF></H4> <B>Name/Address:</B>#Name#, #Address1#, #City#, #State# <BR><B>Contact:</B> #Contact#<BR> <CFELSE> <BR><I>#Name#, #City#</I> </CFIF> </CFOUTPUT> <P>Finally, we can use CFELSEIF to cycle through a number of conditions and produce varying output. Note that you can use CFCASE and CFSWITCH for a more elegant representation of this behavior. <hr> <P> <!--- use CFIF in conjunction with CFELSEIF to specify more than one branch in a conditional situation ---> <CFOUTPUT QUERY="getCenters"> <CFIF city is "San Diego" OR city is "Santa Ana"> <BR><I>#Name#, #City#</I> (this one is in <CFIF city is "San Diego">San Diego<CFELSE>Santa Ana</CFIF>) <CFELSEIF city is "San Francisco"> <BR><I>#Name#, #City#</I> (this one is in San Francisco) <CFELSEIF city is "Suisun"> <BR><I>#Name#, #City#</I> (this one is in Suisun) <CFELSE> <BR><I>#Name#</I> <B>Not in a city we track</B> </CFIF> </CFOUTPUT> </BODY> </HTML>