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.
Archived Comments
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.
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?
> 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".
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.
CFML in 100 minutes has side by side examples of tag versus script. https://github.com/mhenke/C...
@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.
I'll add that clarification about the textile file in the readme. Also CFML in 100 is also open for anyone to help.
@Mike. Thanks for the link. I was looking for something like this. :)
FINALLY.... Adobe Doc has never been able to capture these clearly.... thanks!
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!
Post a pastebin url. :)
Ah yes, pasty bin!
http://pastebin.com/j8Mk9fWG
could you add some additional param examples. Namely the version without name value pairs
This was very useful BTW
Sorry - what? A param w/o name isn't valid. A param w/o a value is just
param name="foo";
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
In ColdFusion 11, this is now built in.
True but we all don't have CF11 yet :( it takes time to migrate. But good to know.