Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created February 24, 2014 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennadel/9187623 to your computer and use it in GitHub Desktop.
Save bennadel/9187623 to your computer and use it in GitHub Desktop.
Robust CFScript Suport For Tags In ColdFusion 11 Beta
<!--- NOTE: ColdFusion 11 was in BETA at the time of this writing. --->
<cfscript>
// Define all of the attributes first. This allows you to
// conditionally build up the attributes to be used.
attributes = {
name: "asyncCode",
action: "run",
thisVar: "Hello",
thatVar: "World"
};
// Invoke script-based tag with collection.
cfthread( attributeCollection = attributes ) {
thread.echoThis = thisVar;
thread.echoThag = thatVar;
}
// Join and output.
cfthread( name = "asyncCode", action = "join" );
writeDump( cfthread );
</cfscript>
<!--- NOTE: ColdFusion 11 was in BETA at the time of this writing. --->
<cfscript>
// Define our friends to be converted into XML.
friends = [
{
name: "Sarah",
age: 47
},
{
name: "Joanna",
age: 35
},
{
name: "Kim",
age: 39
}
];
// Create a short-hand for the buffer output to make the CFXML
// body a bit more readable.
add = writeOutput;
// When you use the ColdFusion tags in CFScript, the body of the
// becomes an output buffer to which you can write the output-body
// of the tag.
cfxml( variable = "doc" ) {
add( "<Friends>" );
for ( friend in friends ) {
add( "<Friend age='#friend.age#'>" );
add( friend.name );
add( "</Friend>" );
}
add( "</Friends>" );
} // END: cfxml.
// Log the XML document to the "remote" service using CFHTTP.
cfhttp(
result = "apiResponse",
method = "post",
url = "http =//127.0.0.1:8500#cgi.script_name#.logger.cfm",
getAsBinary = "yes"
) {
cfhttpParam(
type = "header",
name = "X-Sent-By",
value = "ColdFusion11"
);
cfhttpParam(
type = "body",
value = doc
);
} // END: cfhttp.
// Reset the output buffer. CFContent support finally in CFScript!
cfcontent( type = "text/html;charset=utf-8" );
writeDump( var = doc, label = "Friends" );
</cfscript>
<!--- NOTE: ColdFusion 11 was in BETA at the time of this writing. --->
<cfscript>
// Define page settings. CFSettings support finally in CFScript!
cfsetting(
requestTimeout = 2,
showDebugOutput = true
);
// ------------------------------------------------------ //
// ------------------------------------------------------ //
// Define response headers. CFHeader support finally in CFScript!
cfheader(
name = "X-Served-By",
value = "ColdFusion 11 Beta"
);
// ------------------------------------------------------ //
// ------------------------------------------------------ //
// Set new cookies. CFCookie support finally in CFScript!
cfcookie(
name = "coldFusionLives",
value = "You bet your @$$",
expires = "never"
);
// ------------------------------------------------------ //
// ------------------------------------------------------ //
// Execute binaries. CFExecute support finally in CFScript!
cfexecute(
variable = "standardOutput",
name = "echo",
arguments = "What what!",
timeout = 1
);
// ------------------------------------------------------ //
// ------------------------------------------------------ //
// Build XML documents (via buffer). CFXML support finally in
// CFScript. It's not quite as nice looking at E4X; however, it's
// a heck of a lot better than xmlParse().
cfxml( variable = "doc" ) {
writeOutput( "<Numbers>" );
for ( i = 1 ; i < 5 ; i++ ) {
writeOutput( "<Number>#i#</Number>" );
}
writeOutput( "</Numbers>" );
}
// ------------------------------------------------------ //
// ------------------------------------------------------ //
// Generating PDFs. CFDocument support finally in CFScript.
// ... um, yay, I guess.
cfdocument(
format = "pdf",
filename = expandPath( "./cf11.pdf" ),
overwrite = true
) {
cfdocumentItem( type = "footer" ) {
writeOutput( "Page #cfdocument.currentPageNumber#" );
}
cfdocumentSection() {
writeOutput( "<h1>ColdFusion 11</h1>" );
}
cfdocumentSection() {
writeOutput( "<h1>Getting Started</h1>" );
}
}
// ------------------------------------------------------ //
// ------------------------------------------------------ //
// Process images. While there are a lot of CFIMage methods that
// have been available, I think this is the first time that the
// "WriteToBrowser" action is available in CFScript!
cfimage(
source = "./goose-duck.jpg",
action = "writeToBrowser",
width = 100
);
// ------------------------------------------------------ //
// ------------------------------------------------------ //
// There are other ways to build a dynamic invocation signature
// (such as constructing an argumentCollection object). However,
// I just wanted to see if this would work with cfInvoke.
// --
// NOTE: The ColdFusion 11 Beta documentation states that these
// functions are not available; so, don't use them as they may
// be removed in the final release.
cfinvoke(
returnVariable = "result",
method = "sum"
) {
// Dynamically build up the invocation arguments.
for ( i = 1 ; i <= 10 ; i++ ) {
cfinvokeArgument( name = i, value = i );
}
}
// I am the method we are invoking with a dynamic number of args.
public string function sum() {
// NOTE: Totally overkill (and inefficient) approach - just
// using reduce since it's new in ColdFusion 11.
return(
arguments.reduce(
function( result, item, index, collection ) {
return( result + item );
},
0
)
);
}
// ------------------------------------------------------ //
// ------------------------------------------------------ //
// Invoke a ColdFusion custom tag from CFScript! I don't really
// use custom tags anymore; but, this is an interesting possability.
cf_myTag( variable = "customTagOutput" ) {
writeOutput( "This is my tag content!" );
}
</cfscript>
<!--- NOTE: ColdFusion 11 was in BETA at the time of this writing. --->
<cfscript>
// Previous CFScript implementation of CFQuery.
getFriends = new Query(
sql = "
SELECT
*
FROM
friend
WHERE
id > :id
ORDER BY
id ASC
",
datasource = "testing"
);
getFriends.addParam(
name = "id",
value = 1,
cfSqlType = "cf_sql_integer"
);
writeDump( var = getFriends.execute().getResult(), label = "Friends" );
// ------------------------------------------------------ //
// ------------------------------------------------------ //
// New CFScript implementation of CFQuery.
cfquery(
name = "friends",
datasource = "testing"
) {
writeOutput( "
SELECT
*
FROM
friend
WHERE
id >
" );
cfqueryparam( value = 1, cfSqlType = "cf_sql_integer" );
writeOutput( "
ORDER BY id ASC
" );
}
writeDump( var = friends, label = "Friends" );
// ------------------------------------------------------ //
// ------------------------------------------------------ //
// New CFScript implementation of CFQuery.
// --
// NOTE: When passing the SQL as a tag argument, I do not believe
// that it is possible to actualy use query pamaters, which is why
// I am hard-coding the "1" in the WHERE clause.
cfquery(
name = "friends",
datasource = "testing",
sql = "
SELECT
*
FROM
friend
WHERE
id > 1
ORDER BY
id ASC
"
);
writeDump( var = friends, label = "Friends" );
// ------------------------------------------------------ //
// ------------------------------------------------------ //
// Alternate new function implementation for CFQuery.
// --
// NOTE: The last two arguments are optional.
friends = queryExecute(
"
SELECT
*
FROM
friend
WHERE
id > :id
ORDER BY
id ASC
",
{
id: {
value: 1,
cfSqlType: "cf_sql_integer"
}
},
{
datasource: "testing"
}
);
writeDump( var = friends, label = "Friends" );
</cfscript>
<!--- NOTE: ColdFusion 11 was in BETA at the time of this writing. --->
<cfscript>
// Previous CFScript implementation of CFThread.
thread
action = "run",
name = "AsyncCode"
{
thread.ben = "Jamin";
}
// NOTE: This difference applies to other body-style tags like:
// - CFLock.
// - CFTransaction.
// - CFSaveContent.
// New CFScript implementation of CFThread.
cfthread(
action = "run",
name = "AsyncCode2"
) {
thread.word = "to your mother!";
}
// Join thread using existing syntax.
thread name = "AsyncCode" action = "join";
// Join thread using new syntax.
cfthread( name = "AsyncCode2", action = "join" );
// ------------------------------------------------------ //
// ------------------------------------------------------ //
// Previous CFScript implementation of CFHttp.
apiRequest = new Http(
result = "apiResponse",
method = "post",
url = "http =//127.0.0.1:8500#cgi.script_name#.logger.cfm",
getAsBinary = "yes"
);
apiRequest.addParam(
type = "header",
name = "X-Sent-By",
value = "ColdFusion11"
);
apiResponse = apiRequest.send();
// New CFScript implementation of CFHttp.
cfhttp(
result = "apiResponse",
method = "post",
url = "http =//127.0.0.1:8500#cgi.script_name#.logger.cfm",
getAsBinary = "yes"
) {
cfhttpParam(
type = "header",
name = "X-Sent-By",
value = "ColdFusion11"
);
} // END: cfhttp.
// ------------------------------------------------------ //
// ------------------------------------------------------ //
// Previous CFScript implementation of CFParam that is totally
// broken and you should only use this syntax if you don't like
// your fellow programmers. No, for real, don't use this syntax.
// It's not cool. And people will make fun of you.
param url.groove = "sauce";
// Previous CFScript implementation of CFParam.
param name = "url.ben" type = "string" default = "jamin";
// New CFScript implementation of CFParam.
cfparam( name = "url.foo", type = "string", default = "bar" );
// ------------------------------------------------------ //
// ------------------------------------------------------ //
// Previous CFScript implementation of CFInclude.
include "./code.cfm";
// New CFScript implementation of CFInclude.
cfinclude( template = "./code.cfm" );
// ------------------------------------------------------ //
// ------------------------------------------------------ //
// Previous CFScript implementation of CFThrow.
throw( type = "Fail", message = "Ooops." );
// New CFScript implementation of CFThrow.
cfthrow( type = "Fail", message = "Ooops." );
// ------------------------------------------------------ //
// ------------------------------------------------------ //
// Previous CFScript implementation of CFExit.
exit "exitTemplate";
// New CFScript implementation of CFExit.
cfexit( method = "exitTemplate" );
// ------------------------------------------------------ //
// ------------------------------------------------------ //
// Previous CFScript implementation of CFAbort.
abort "Something went wrong";
// New CFScript implementation of CFAbort.
cfabort( showError = "Something went wrong" );
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment