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.

Name Type Required Default Description
position any No last The position in the section's stack where you want the content placed. Valid values are first, last, or the numeric position.
overwrite any No false Whether or not to overwrite any of the content. Valid values are false, true, or all.
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!