Contact Us or call 1-877-932-8228


ColdFusion's <cfsavecontent> tag provides another mechanism for storing data in a variable. The syntax is as follows:

<cfsavecontent variable="var_name"> Variable value </cfsavecontent>

The great thing about <cfsavecontent> is that it can contain other ColdFusion code such as conditionals and loops. This makes it easy to save the results of complicated code as a string of text without appending to a variable with <cfset>.

The following example illustrates the value of <cfsavecontent>.

Code Sample:

<title>cfsavecontent Demo</title>
<cfset beatles = ArrayNew(1)>
<cfset ArrayAppend(beatles,"John")>
<cfset ArrayAppend(beatles,"Paul")>
<cfset ArrayAppend(beatles,"George")>
<cfset ArrayAppend(beatles,"Ringo")>

<h1>Store Beatles as HTML List</h1>

<h2>With cfset</h2>
<cfset BeatlesList = "<ul>">
<cfloop from="1" to="#ArrayLen(beatles)#" index="i">
	<cfset BeatlesList = BeatlesList & "<li>" & beatles[i] & "</li>">
<cfset BeatlesList = BeatlesList & "</ul>">


<h2>With cfsavecontent</h2>
<cfsavecontent variable="BeatlesList2">
	<cfloop from="1" to="#ArrayLen(beatles)#" index="i">


As you can see from the bolded sections, setting and changing values with <cfset> can become ugly and <cfsavecontent> provides a much more elegant solution.