the singularity of being and nothingness
Posts tagged CFScript
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 >
Renaming Files in CFScript
May 13th
I’ve recently taken to writing all of my components in pure cfscript. To me, they look cleaner and are easier to navigate. Plus, I just really like cfscript 🙂
One of the challenges, of course, is that after using tags for so long, it can be sometimes challenging to find equivalent functionality in cfscript. Some tags expose functionality that may not be readily apparent in cfscript functions provided.
Take cffile’s “rename” action. If you look through the list of “FileXXXX()” methods, you’ll see a lot that correspond 1-to-1 with the tag actions. However, interestingly enough, there’s no FileRename() function.
Now you might think that this means that files can’t be renamed with using cffile. Wrong! You can simply use FileMove() to accomplish the rename. And actually, if you think about it, this makes sense. After all, when you move a file, you literally move it–so if the file is moved to a different or same location with a new file name, that’s the file that’s getting moved…no copy to worry about cleaning up after the face.
Ok, enough of that. So here’s a pretty ordinary file rename with cffile:
<cffile action=”rename” source=”c:\files\memo\readonlymemo.doc” destination=”c:\files\memo\normalmemo.doc”>
But we can accomplish precisely the same thing with FileMove():
filemove(c:\files\memo\readonlymemo.doc, c:\files\memo\normalmemo.doc);
Pretty nice!
Share this:
CFScript Alternative to CFHeader
Oct 30th
In another post, I wrote about how you can replicate the functionality of cfcontent’s “reset” attribute in CFScript. The other day, I came across a related issue of needing to be able to do the same thing (all code in CFScript, that is) for cfheader. Fortunately, by using the same approaching of tapping into the underlying Java methods, this is extremely easy.
Setting StatusTo set the status code for the page request, simply do the following:
pc = getpagecontext().getresponse(); pc.getresponse().setstatus(301);
No big deal there. Also, be aware that the setstatus() method can take an optional second string attribute, so if you wish to define a message to accompanying the status code, feel free 🙂
Setting HeadersHonestly, I use headers in CF mostly for downloading files, or returning certain content types. Just as with setting the status, it’s pretty freaking simply to do in CFScript:
pc = getpagecontext().getresponse(); pc.getresponse().setcontenttype('application/vnd-ms.excel'); pc.setHeader("Content-Disposition","inline;filename=MyFile.xls");Share this:
CFScript Alternative to CFContent
Jul 29th
Ran into this issue today. At work, data returned from CFCs have a bunch of content pre-pended. In regular tag-flow coding, this is super-simple to overcome:
<cfcontent type="text/plain" reset="true" /> <cfreturn mycontent />
Using the cfcontent tag, I can “[discard] output that precedes call to cfcontent” (from the docs :)). This strips out all the garbage that I don’t want, and returns only the awesome data that I need.
Since we’re getting ready to upgrade to CF9, however, I’ve been playing around with writing my ColdFusion components in the new 100% script-based syntax. One problem, though: there is no cfscript version of cfcontent, so I’m back to the original problem.
Fortunately, there is a solution. By tapping into the underlying Java methods that are exposed, we can easily recreate similar functionality. Here’s all it takes:
remote any function getdata() { getpagecontext().getcfoutput().clearall(); � return supersweetdata; }
Nothing to it. And for future reference, take 5 minutes to dump out getpagecontext(). You can see all the nice methods that are available…who knows, you might just find something useful 🙂
Share this: