Understanding apex:define Component in Visualforce

The <apex:define> component is used within a Visualforce template to define specific content that should be inserted into a region specified by <apex:insert> in the template. It’s a key part of creating templated layouts in Visualforce.


Why Use <apex:define>?

  • Helps structure and organize Visualforce pages using a common layout.
  • Promotes reusability by separating layout from content.
  • Allows developers to define page sections like headers, footers, and body content.

How It Works

  • A template page contains <apex:insert> tags to mark regions.
  • A content page that uses the template defines those regions using <apex:define>.

Template Page (PageTemplate.page)

xmlCopyEdit<apex:composition template="StandardLayout">
    <apex:define name="body">
        <h1>This is the body content</h1>
    </apex:define>

    <apex:define name="footer">
        <p>This is the footer section</p>
    </apex:define>
</apex:composition>

Template Layout (StandardLayout.page)

xmlCopyEdit<apex:component>
    <apex:insert name="body"/>
    <hr/>
    <apex:insert name="footer"/>
</apex:component>

Key Attributes

  • name: The name of the section you want to define (must match the name used in <apex:insert>).
  • Content inside <apex:define> replaces the corresponding <apex:insert> in the template.

Use Cases

  • Consistent Layouts: Maintain the same layout across multiple Visualforce pages.
  • Dynamic Page Sections: Customize specific regions of a page while reusing common elements.
  • Theming and Branding: Easily apply consistent headers/footers across an application.