Friday, June 1, 2012

Template using in Visualforce


Template using in Visualforce page:
Template is nothing but CSS and page components. In sales force we can define template in VF pages using following tags.
  1. Composition Tag (<apex: composition>):
        This tag contains at least one child tag is call <apex: define> and which import in another VF page using <apex: insert> tag.  Given below example for this tag.
  1. Rerencing Existing page <apex:include>:
    Use the <apex:include> tag when you want to duplicate the entire content of another page without making any changes.You can use this technique to reference existing markup that will be used the same way in several locations.

VFCode:
·        Create CallCompositionPage visualforce page:
<apex:page controller="CompositionController">
<apex:form >
 <apex:insert name="First Name"></apex:insert>
 <br/><br/>
 <apex:insert name="Last Name"></apex:insert>
 <br/><br/>
 <apex:insert name="Age"></apex:insert>
 <br/><br/>
 <apex:insert name="Address"></apex:insert>
 <br/><br/>
 <apex:commandButton action="{!save}" value="Save" id="saveButton"/>
</apex:form>
</apex:page>

·        Create compositionPage visualforce page:
<apex:page controller="CompositionController">
 <apex:composition template="{!myTemplate}">
  <apex:define name="First Name">
  <apex:outputLabel value="First Name" for="First Name"></apex:outputLabel>
  <apex:inputText id="fname" value="{!fname}"/>
 </apex:define>
 <apex:define name="Last Name">
  <apex:outputLabel >Last Name</apex:outputLabel>
  <apex:inputText id="lname" value="{!lname}"/>
 </apex:define>
 <apex:define name="Age">
  <apex:outputLabel >Age</apex:outputLabel>
  <apex:inputText id="age" value="{!age}"/>
 </apex:define>
 <apex:define name="Address">
  <apex:outputLabel >Address</apex:outputLabel>
  <apex:inputText id="address" value="{!address}"/>
 </apex:define> 
 </apex:composition>
</apex:page>
·         Write controller class CompositionController:

public class CompositionController {
String fname;
String lname;
Integer age;
String address;
// Dynamic Template Name
public PageReference getmyTemplate() {
return Page.CallCompositionPage;
}
// Action button Save
public PageReference save() {
return null;
}
public void setFname(String fname) {
fname = fname;
}
public String getFname() {
return fname;
}
public void setLname(String lname) {
lname = lname;
}
public String getLname() {
return lname;
}
public void setAge(Integer age) {
age= age;
}
public Integer getAge() {
return age;
}
public void setAddress(String address) {
address= address;
}
public String getAddress() {
return address;
}
}

No comments:

Post a Comment