How to Use a Template with Another Visualforce Page

How to Use a Template with Another Visualforce Page

Step 1. Create a new Visualforce page called BasicTemplate.

<apex:page>
<apex:stylesheet value=”http://developer.force.com/workbooks/vfdemo.css”/>
<h1>My Fancy Site</h1>
<apex:insert name=”body”/>
</apex:page>

The key here is the <apex:insert> component. You won’t visit this page (unless developing it) directly. Rather, create another Visualforce page that embeds this template, inserting different values for each of the <apex:insert> components.
Note that each such component is named. In the above template, you have a single insert position named body. You can have dozens of such positions.
Step 2: Use a Template with Another Page
You can now embed the template in a new page, filling in the blanks as you go.
Create a new Visualforce page called MainPage.
Within the page, add the following markup:

<apex:page sidebar=”false” showHeader=”false”>
<apex:composition template=”BasicTemplate”>
<apex:define name=”body”>
<p>This is a simple page demonstrating that this
text is substituted, and that a banner is created.</p>
</apex:define>
</apex:composition>
</apex:page>

The <apex:composition> component fetches the Visualforce template page you created earlier, and the <apex:define> component fills the named holes in that template. You can create multiple pages that use the same component, and just vary the placeholder text.

Leave a Reply