Back
contentFor()
contentFor() is used to store a section's output in a layout. It allows you to define content in your view templates and then render it in a layout using #includeContent()#. The function maintains a stack for each section, so multiple pieces of content can be added in a controlled order.
1. Basic usage
<!--- In your view --->
<cfsavecontent variable="mySidebar">
<h1>My Sidebar Text</h1>
</cfsavecontent>
<cfset contentFor(sidebar=mySidebar)>
<!--- In your layout --->
<html>
<head><title>My Site</title></head>
<body>
<cfoutput>
#includeContent("sidebar")# <!-- Renders the sidebar content -->
#includeContent()# <!-- Renders main content -->
</cfoutput>
</body>
</html>
2. Adding multiple pieces to the same section
<cfset contentFor(sidebar="First piece of content")>
<cfset contentFor(sidebar="Second piece of content", position="first")>
<!--- Renders 'Second piece of content' first, then 'First piece of content' -->
#includeContent("sidebar")#
3. Overwriting content
<cfset contentFor(sidebar="Old content")>
<cfset contentFor(sidebar="New content", overwrite=true)>
<!--- Only 'New content' will be rendered -->
#includeContent("sidebar")#
Copied!