Created
February 24, 2014 12:36
-
-
Save bennadel/9187623 to your computer and use it in GitHub Desktop.
Robust CFScript Suport For Tags In ColdFusion 11 Beta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- 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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment