View Helpers
contentFor()
Signature
Section titled “Signature”contentFor() — returns void
Available in: controller
Category: Miscellaneous Functions
Description
Section titled “Description”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.
Parameters
Section titled “Parameters”| 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. |
Examples
Section titled “Examples”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")#