I saw this type of code popping up in some Railo source code, but for some reason did not use it before.
In javascript, I was already using it a long time: var a = (b>100) ? 100 : b, but in Railo? Not yet.
Well, that has changed now! It is much simpler then using IIF(), and has great power:
<cfset testValue = 107 />
<cfset nothigherThen100 = testValue gt 100 ? 100 : testValue />
And you can also use it inline:
<cfset testValue = 107 />
<cfoutput>#testValue# is #(testValue gt 100 ? 'greater then' : 'smaller or equal to')# hundred</cfoutput>
Off course, you can nest these statements:
<cfoutput>#testValue# is #(testValue gt 100 ? 'greater then' : (testValue eq 100 ? 'exactly' : 'smaller then'))# hundred</cfoutput>
If you are new to this concept, the syntax is really simple, once you get the hang of it: (also see Wikipedia)
[statement, evaluated to true or false] ? [value if statement is true] : [value if statement is false]
Pretty nifty eeh? [edit] And it works in Adobe Coldfusion 9 too. [/edit]
#1 by Big Mad Kev - December 7, 2010 at 1:04 PM
http://www.bennadel.com/blog/1643-Learning-ColdFusion-9-The-Ternary-Operator.htm
Very handy indeed
#2 by Paul Klinkenberg - December 7, 2010 at 1:29 PM
Anyway, I'm still happy to have blogged about it, because this option just rocks! :-)
#3 by Todd Rafferty - December 7, 2010 at 2:20 PM
#4 by Paul Klinkenberg - December 7, 2010 at 2:34 PM
#5 by Todd Rafferty - December 7, 2010 at 2:38 PM
#6 by kristof polleunis - February 6, 2011 at 1:28 AM