Wednesday, March 11, 2009

Large strings in ColdFusion

ColdFusion is great. Generating complex html with CF is very easy but sometimes I'm not interested in creating a complex html document but, rather, I need to create a large and complex string and maybe I want to write it to a file.
To work with strings in CF as easily as I work with html I wrote a very simple custom tag, <cf_BlockToString>.
The tag works like this:
<cf_BlockToString var="myVar">
This is a bunch of text where I'm able to "quote" things and #pound# out the special characters without escaping.
<cfset newVar="I can still do normal CF stuff too."> <cfoutput>#newVar#</cfoutput> </cf_BlockToString>

<cfoutput>#myVar#</cfoutput>
The quick and dirty version (no error checking) of BlockToString.cfm is:
<cfif thisTag.ExecutionMode is 'end'>
<cfset Caller."#Attributes.var#" = thisTag.GeneratedContent>

<cfset thisTag.GeneratedContent = "">
</cfif>
Have fun.