the singularity of being and nothingness
Posts tagged CFSwitch

ColdFusion Gotcha: Switch Statement in CFScript
Aug 30th
The other day, I was reworking a bit of tag-based code to use my preference of <cfscript> syntax. While doing this, I ran across something like this:
<cfswitch expression="#img.extension#"> <cfcase value=".gif,.jpg"> Not transparent </cfcase> <cfcase value=".png"> Transparent </cfcase> </cfswitch>
No rocket-science here, just a simple switch statement which just so happens to have a “case” which evaluates a list of values. Honestly, I’m not sure I had ever done a list of values in a <case>, but it just works.
Thinking nothing of it, I quickly converted it to <cfscript> syntax:
<cfscript> switch(img.extension) { case ".gif,.jpg": writeoutput("Not transparent"); break; case ".png": writeoutput("Transparent"); break; } </cfscript>
At first, I thought it was working. But as I tested a bit more, I noticed that it wasn’t. No error, per se, but the “case” was not evaluating as it did in the tag-based equivalent.
Turns out you have to do something a bit different with <cfscript>. Here’s an alternative that will replicate the tag-based functionality:
<cfscript> switch(img.extension) { case ".gif": case ".jpg": writeoutput("Not transparent"); break; case ".png": writeoutput("Transparent"); break; } </cfscript>
According to the docs,
Multiple case constant: statements can precede the statement or statements to execute if any of the cases are true. This lets you specify several matches for one code block.
Ah, so More >