Understanding apex:include Component in Visualforce

The <apex:include> component is used to embed reusable Visualforce pages inside another Visualforce page. This allows for modularity and code reuse, simplifying your page management.


Why Use <apex:include>?

  • Embed the content of one Visualforce page into another.
  • Helps in reducing code duplication by centralizing content in one place.
  • Makes it easier to manage and update reusable components.

Basic Syntax

xmlCopyEdit<apex:include pageName="pageName"/>
  • pageName: The name of the Visualforce page to include.

Example: Including a Header Page in Multiple Pages

xmlCopyEdit<apex:page>
    <apex:include pageName="HeaderPage"/>
    <h1>Welcome to My Visualforce Page!</h1>
</apex:page>

HeaderPage Visualforce Example:

xmlCopyEdit<apex:page>
    <header>
        <h2>My Company Header</h2>
        <nav>Home | About | Contact</nav>
    </header>
</apex:page>

Key Points

  • The included page can be dynamic and accept parameters from the parent page.
  • Useful for repeating elements like headers, footers, and sidebars.
  • Ensures consistency across pages by reusing common components.

Use Cases

  • Header and footer sections that appear on multiple pages.
  • Embedded navigation or dynamic content.
  • Simplifying page structure for better maintainability.