ColdFusion Notes
General notes on ColdFusion:
-
cfscript reference: http://www.adobepress.com/articles/article.asp?p=1179656
-
Comment syntax CFML: a less than sign + exclamation mark + three dashes for comments, i.e. html comment with an extra dash
-
Comment syntax cfscript: It is different! It's pretty much java, so use //
-
NOT syntax - CF can use the "!" for NOT syntax.
-
MOD - e.g. (11 MOD 3)
-
Concatenate: is an amperstand, "&"; append "&=" is equivalent to a = a & b
-
Sleep - < cfset sleep(5000) > - for 5 seconds, i.e. milliseconds
-
Increment - CF8 added the ++ operator! E.g. < cfset kount++ / > (note spaces added to not get this text lost)
-
Implicitly create structs and arrays in CF8+ with { } and [ ]
-
Escape special characters, e.g. quotes and hashes in a string by repeating it (for large amounts of text, consider cfsavecontent)
-
Escape for HTML - HTMLEditFormat(thestring)
-
evaluate() is a thing, but array notation is safer / recommended: ProductName: #Form["product_" & i]#
-
Record Count of a query: queryname.RecordCount
-
Alphabet Loop - < cfloop index="i" from="65" to="90" >< a href="m.cfm&kword=#chr(i)#">#chr(i)#< /a >< /cfloop >
-
Variable types: numeric, string, boolean...
-
Get generated SQL: qryName.getMetaData().getExtendedMetaData().sql
-
Some good basic introduction to CFML - http://www.depressedpress.com/Content/Development/ColdFusion/Guides/Variables/Index.cfm
-
ColdFusion + Consume Web Service - http://www.adobe.com/livedocs/coldfusion/6/Developing_ColdFusion_MX_Applications_with_CFML/webservices4.htm
-
Convert query to a List (e.g. for IN SQL):
-
< cfset x="ValueList(getProductId.productId)" >< cfset x="ValueList(getProductId.productId)" >< /cfset >< /cfset >
-
Better: for string values, see ListQualify().
-
Query NULL values are converted to empty strings - i.e. do not try and use IsNull on them!
-
Best practices: http://wiki.coldbox.org/wiki/DevelopmentBestPractices.cfm
-
Always use "var" to force a scope on variables inside a function.
-
< cfabort > terminates a page.
-
IsDefined Example: < cfif isDefined("session.button_active") >
-
Dates:
-
CF will guess at the date from a string - e.g. < cfset d2="13/3/2007" >, http://blog.stannard.net.au/2006/10/25/date-objects-vs-date-strings-in-coldfusion/
-
But much safer to be explicit - - http://www.bennadel.com/blog/811-Converting-ISO-Date-Time-To-ColdFusion-Date-Time.htm
-
Get a human readable format: dateformat(news_Date,"yyyy-mm-dd")
-
And time: TimeFormat(todayDate, "HH:mm:ss")
-
You can call a function and identify the arguments, e.g.(from your 'Calling user-defined functions' page): #TotalInterest(principal=Form.Principal, annualPercent=Form.Percent, months=Form.Months)#
-
Arrays start at 1. You can address an element directly: myarray[3]
-
Object Orientated CF (components) - good tutorial - http://objectorientedcoldfusion.org
-
CFIF - there is no slash before cfelse or cfelseif - e.g. < cfif expression >A< cfelseif expression >B< cfelse >C< /cfif >
-
structKeyExists() vs IsDefined() - structKeyExists() is generally better, more precise - isDefined can be fooled - https://forums.adobe.com/thread/491991
-
Structs - watch out for trailing spaces in key names.
-
cflocation immediately ends the rest of the script processing - guy tests and has cfheader workaround - https://www.bennadel.com/blog/1550-performing-coldfusion-processing-after-a-cflocation-tag.htm
-
cfsavecontent variable="html_blob" - syntax for long variable definition, somewhat like HEREDOC
Cross Platform Programming:
Apparently "/" will work on Windows and Linux (I guess the original Allaire developers were UNIX!). Or use ExpandPath().
Also, best to be case sensitive. Windows doesn't care. Linux does. Apparenlty MySQL on Linux does in places where Windows version isn't. Weird.
https://www.raymondcamden.com/2009/05/05/Cross-Operating-System-ColdFusion-Development-Guide-First-Draft
Coldfusion: Command line handler for Windows - https://www.ortussolutions.com/products/commandbox - https://docs.lucee.org/guides/getting-started/commandbox.html
Troubleshooting Tips:
-
cfoutput - nesting cfoutput can be completely wrong some times
-
< cflog file="LC-LOG" text="test-cflog.cfm - BOF" > - cflog, possibly in Railo and not Adobe?
-
cfdump and writedump - writedump is for inside cfscript, i think.
Tags: cfml, cold, fusion, coldfusion, cf, railo, lucee