Examples of ColdFusion 9 Script Support

This post is more than 2 years old.

A few weeks ago I posted a simple guide to dealing with features you could not use in ColdFusion 9 script based code. While CF9 went a long way to making the scripting form more powerful, there are still some holes that need patching. One of the things that can be a bit confusing though is figuring out all the new script based forms of tags we've used in the past. While not a deep dive, I decided to write up a quick template that ran through all of these new keywords just so I'd have the syntax handy. I hope this helps.

Doing a dump...
<cfscript> writedump(var=server,top=2,label="You betcha"); </cfscript>

Doing an include...
<cfscript> include "foo.cfm"; //a dynamic version... x = "test2.cfm"; include x; </cfscript>

Doing a cflocation...
<cfscript> location(url="test2.cfm",addtoken=false); </cfscript>

cfparam
<cfscript> param name="y" default=1 min=1; writeOutput("y is " & y); </cfscript>

Doing a lock...
<cfscript> lock type="exlcusive" name="somelock" timeout="30" { //stuff } </cfscript>

Doing a log...
<cfscript> writelog(file="application", text="beer time?"); </cfscript>

Doing cfsavecontent...
<cfscript> savecontent variable="buffer" { include "test2.cfm"; } </cfscript>

Exception handling...
<cfscript> try { writeoutput("unknown #variablename#"); } catch(any e) { //if(e.errnumber == 0) rethrow; writedump(var=e); } finally { writeoutput("<p>finally...."); } </cfscript>

Tracing...
<cfscript> trace(category="beer",text="my trace"); </cfscript>

Threading...
<cfscript> thread name="slowthing" priority="low" { sleep(1000); } </cfscript>

transactions...
<cfscript> transaction action="begin" {

//query

} </cfscript>

Throwing an exception...
<cfscript> throw(message="You smell", detail="No, you REALLY smell"); </cfscript>

Stopping the execution of a request
<cfscript> abort; </cfscript>

I think that covers everything, but if I missed something, let me know.

Shoot - one more just occurred to me. You can set pagencoding for a CFC at the top of the file - but after the component begins, ala:

component car { pageencoding "Cp1252" }

I've yet to use that syntax.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Phillip Senn posted on 4/25/2011 at 5:58 PM

I think the main purpose of ColdFusion is to be a bridge between HTML and the database, so a list of keywords such as this should talk about cfquery vs new query/setSQL. Or you could say it's been replaced by EntityLoad or ORMExecuteQuery.

Comment 2 by Raymond Camden posted on 4/25/2011 at 6:02 PM

I wanted to focus on keywords in the language, not the new components that were built to replace the tag versions.

I would _not_ say ORM has replaced traditional cfquery. It's an option. I'd say - in most cases - I prefer ORM - but the presence of ORM does not mean that normal queries are not used anymore.

Just my two cents. ;)

Would a blog post on the components be useful as well?

Comment 3 by Phillip Senn posted on 4/25/2011 at 6:10 PM

> Would a blog post on the components be useful as well?

You have to ask?
You know, I was just thinking the other day about when I got started in CFML and Raymond Camden was a lone voice in the wilderness. For me, someone who thirsts after ANYTHING related to CF, I am still grateful for every nugget. I remember listening to http://www.helmsandpeters.com/ and perk up when one of them would say "cfset".

Comment 4 by Raymond Camden posted on 4/25/2011 at 6:12 PM

I'll whip up some examples. As it stands, I use Snippets in CFB to help me remember the syntax for script based queries. I can't memorize it for some reason.

Comment 5 by Mike Henke posted on 4/25/2011 at 6:13 PM

CFML in 100 minutes has side by side examples of tag versus script. https://github.com/mhenke/C...

Comment 6 by Raymond Camden posted on 4/25/2011 at 6:16 PM

@Mike: Nice. I have to admit - I almost didn't click on the .textfile file. I assumed that was some binary format for an app I didn't have. It wasn't obvious at all I could just click and view in the browser.

Comment 7 by Mike Henke posted on 4/25/2011 at 6:21 PM

I'll add that clarification about the textile file in the readme. Also CFML in 100 is also open for anyone to help.

Comment 8 by Mihai Baboi posted on 4/25/2011 at 9:20 PM

@Mike. Thanks for the link. I was looking for something like this. :)

Comment 9 by Henry posted on 4/25/2011 at 9:41 PM

FINALLY.... Adobe Doc has never been able to capture these clearly.... thanks!

Comment 10 by Mark Gregory posted on 4/26/2011 at 3:38 AM

Tried to post cfflush, cfswitch and cfhttp with formfield params, but got flagged as spam.

I think I would die without cfswitch in script these days!

Comment 11 by Raymond Camden posted on 4/26/2011 at 3:47 AM

Post a pastebin url. :)

Comment 12 by Mark Gregory posted on 4/26/2011 at 7:58 AM
Comment 13 by James Mohler posted on 4/29/2012 at 2:07 AM

could you add some additional param examples. Namely the version without name value pairs

This was very useful BTW

Comment 14 by Raymond Camden posted on 4/30/2012 at 6:09 PM

Sorry - what? A param w/o name isn't valid. A param w/o a value is just

param name="foo";

Comment 15 by Nathanael Waite posted on 7/29/2014 at 3:50 AM

here is how to call a cfmodule or cftag in cfscript. Comments taken from various other blogs. Hope this save other some time.

module= createObject("java","coldfusion.tagext.lang.ModuleTag");
module.setPageContext(getPageContext());
module.setTemplatePath(expandPath('tagname.cfm')); // cfmodule or cftag location
module.setAttributeCollection({varone="123",vartwo=2,varthree="abc"}); // attributes passed to the tag.

savecontent variable="moduleoutput" {
module.doStartTag();
module.doEndTag();
}

module.releasetag();

writeoutput(moduleoutput); // moduleoutput contains tag output

Comment 16 by Raymond Camden posted on 7/29/2014 at 6:21 AM

In ColdFusion 11, this is now built in.

Comment 17 by Nathanael Waite posted on 7/29/2014 at 10:49 PM

True but we all don't have CF11 yet :( it takes time to migrate. But good to know.