Newer Version Available

This content describes an older version of this product. View Latest

Defining Templates with <apex:composition>

All templates defined using <apex:composition> must have one or more child <apex:insert> tags. An <apex:insert> tag indicates to pages that import the template that a section needs a definition. Any Visualforce page that imports a template using <apex:composition> must use <apex:define> to specify the content of each <apex:insert> section of the template.

You can create a skeleton template that allows subsequent Visualforce pages to implement different content within the same standard structure. To do so, create a template page with the <apex:composition> tag.

The following example shows how you can use <apex:composition>, <apex:insert>, and <apex:define> to implement a skeleton template.

First, create an empty page called myFormComposition that uses a controller called compositionExample:
1<apex:page controller="compositionExample">
2
3</apex:page>
After saving the page, a prompt appears that asks you to create compositionExample. Use the following code to define that custom controller:
1swfobject.registerObject("clippy.codeblock-1", "9");public class compositionExample{
2
3    String name;
4    Integer age;
5    String meal;
6    String color;
7    
8    Boolean showGreeting = false;
9    
10    public PageReference save() {
11        showGreeting = true;
12        return null;
13    }
14    
15    public void setNameField(String nameField) {
16        name = nameField;
17    }
18    
19    public String getNameField() {
20        return name;
21    }
22    
23    public void setAgeField(Integer ageField) {
24        age= ageField;
25    }
26    
27    public Integer getAgeField() {
28        return age;
29    }
30    
31    public void setMealField(String mealField) {
32        meal= mealField;
33    }
34    
35    public String getMealField() {
36        return meal;
37    }   
38         
39    public void setColorField(String colorField) {
40        color = colorField;
41    }
42    
43    public String getColorField() {
44        return color;
45    }       
46    
47    public Boolean getShowGreeting() {
48        return showGreeting;
49    }
50}
Next, return to myFormComposition and create a skeleton template:
1swfobject.registerObject("clippy.codeblock-2", "9");<apex:page controller="compositionExample">
2    <apex:form >
3        <apex:outputLabel value="Enter your name: " for="nameField"/>
4        <apex:inputText id="nameField" value="{!nameField}"/>
5        <br />
6        <apex:insert name="age" />
7        <br />
8        <apex:insert name="meal" />
9        <br />      
10        <p>That's everything, right?</p>
11        <apex:commandButton action="{!save}" value="Save" id="saveButton"/>
12    </apex:form>
13</apex:page>
Notice the two <apex:insert> fields requiring the age and meal content. The markup for these fields is defined in whichever page calls this composition template.
Next, create a page called myFullForm, which defines the <apex:insert> tags in myFormComposition:
1swfobject.registerObject("clippy.codeblock-3", "9");<apex:page controller="compositionExample">
2    <apex:messages/>
3    <apex:composition template="myFormComposition">
4    
5    <apex:define name="meal">
6        <apex:outputLabel value="Enter your favorite meal: " for="mealField"/>
7        <apex:inputText id="mealField" value="{!mealField}"/>
8    </apex:define>
9
10    <apex:define name="age">
11        <apex:outputLabel value="Enter your age: " for="ageField"/>
12        <apex:inputText id="ageField" value="{!ageField}"/>
13    </apex:define>
14    
15   <apex:outputLabel value="Enter your favorite color: " for="colorField"/>
16   <apex:inputText id="colorField" value="{!colorField}"/>
17    
18    </apex:composition>
19    
20    <apex:outputText id="greeting" rendered="{!showGreeting}" value="Hello {!nameField}. 
21    You look {!ageField} years old. Would you like some {!colorField} {!mealField}?"/>
22</apex:page>
Notice the following about the markup:
  • When you save myFullForm, the previously defined <apex:inputText> tags and Save button appear.
  • Since the composition page requires age and meal fields, myFullForm defines them as text input fields. The order in which they appear on the page does not matter; myFormComposition specifies that the age field is always displayed before the meal field.
  • The name field is still imported, even without a matching <apex:define> field.
  • The color field is disregarded, even though controller code exists for the field. This is because the composition template does not require any field named color.
  • The age and meal fields do not need to be text inputs. The components within an <apex:define> tag can be any valid Visualforce tag.
To show how you can use any valid Visualforce in an <apex:define> tag, create a new Visualforce page called myAgelessForm and use the following markup:
1swfobject.registerObject("clippy.codeblock-4", "9");<apex:page controller="compositionExample">
2    <apex:messages/>
3    <apex:composition template="myFormComposition">
4    
5    <apex:define name="meal">
6        <apex:outputLabel value="Enter your favorite meal: " for="mealField"/>
7        <apex:inputText id="mealField" value="{!mealField}"/>
8    </apex:define>
9
10    <apex:define name="age">
11        <p>You look great for your age!</p>
12    </apex:define>
13
14    </apex:composition>
15    
16    <apex:outputText id="greeting" rendered="{!showGreeting}" value="Hello {!nameField}. 
17    Would you like some delicious {!mealField}?"/>
18</apex:page>
Notice that the composition template only requires an <apex:define> tag to exist. In this example, age is defined as text.

Dynamic Templates

A dynamic template allows you to assign a template through a PageReference. The template name is assigned to a controller method that returns a PageReference containing the template you want to use.

For example, create a page called myAppliedTemplate that defines the skeleton template:
1<apex:page>
2    <apex:insert name="name" />
3</apex:page>
Next, create a controller called dynamicComposition with a method that will return a reference to this page:
1public class dynamicComposition {
2    public PageReference getmyTemplate() {
3        return Page.myAppliedTemplate;
4    }
5}
Last, create a page called myDynamicComposition that implements this controller and the dynamic template:
1<apex:page controller="dynamicComposition">
2    <apex:composition template="{!myTemplate}">
3    <apex:define name="name">
4        Hello {!$User.FirstName}, you look quite well.
5    </apex:define>
6    </apex:composition>
7</apex:page>