<cfsavecontent>

Contact Us or call 1-877-932-8228
<cfsavecontent>

<cfsavecontent>

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:

FlowControl/Demos/cfsavecontent.cfm
<html>
<head>
<title>cfsavecontent Demo</title>
</head>
<cfset beatles = ArrayNew(1)>
<cfset ArrayAppend(beatles,"John")>
<cfset ArrayAppend(beatles,"Paul")>
<cfset ArrayAppend(beatles,"George")>
<cfset ArrayAppend(beatles,"Ringo")>

<body>
<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>">
</cfloop>
<cfset BeatlesList = BeatlesList & "</ul>">

<cfoutput>#BeatlesList#</cfoutput>

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

<cfoutput>#BeatlesList2#</cfoutput>
</body>
</html>

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.

Next