Search This Blog

Friday, April 20, 2012

ColdFusion: Immediate If vs. Ternary Operator

ColdFusion 9 introduces a new operator called ternary operator. Unlike iif(), ternary operator looks cleaner and less confusing.
See the snippet below. Pay attention on the quotes.

<cfparam name="condition" default="true">

<cfset var_1 = "TRUE">
<cfset var_2 = "FALSE">

<cfoutput>
 <!--- Use de() and quote will not evaluate the variable --->
 immediate if: #iif( (condition), de('var_1'), de('var_2') )#
 <br />
 <!--- Use quote 'var_1' or de(var_1) will evaluate the variable --->
 immediate if: #iif( (condition), 'var_1', 'var_2' )#
 <br />

 <!--- Use quote 'var_1' will not evaluate the variable --->
 ternary operator: #( (condition) ? 'var_1' : 'var_2' )#
 <br />
 <--- No quote var_1 will evaluate the variable --->
 ternary operator: #( (condition) ? var_1 : var_2 )#
</cfoutput>

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.