Please let me know the difference between apex:composition and apex:component

Help is much appreciated..



Composition: An area of a page that includes content from a second template page. Template pages are Visualforce pages that include one or more components. The component names the associated template, and provides body for the template's components with matching components. Any content outside of an component is not rendered

This is Page which define template and use composition element

<!-- Page: composition -->
<!-- This page acts as the template. Create it first, then the page below.  --> 
    <apex:outputText value="(template) This is before the header"/><br/>
    <apex:insert name="header"/><br/>
    <apex:outputText value="(template) This is between the header and body"/><br/>
    <apex:insert name="body"/>

This is Page which define elements of composition

<!-- Page: page -->
    <apex:composition template="composition">
        <apex:define name="header">(page) This is the header of mypage</apex:define>
        <apex:define name="body">(page) This is the body of mypage</apex:define>

Component: Components are reusable section of UI. For ex. Header, footer, widgets etc.

This is Page:

    <c:myComponent myValue="My component's value" borderColor="red" />

This is Component Definition:

    <apex:attribute name="myValue" description="This is the value for the component."
        type="String" required="true"/>
    <apex:attribute name="borderColor" description="This is color for the border." type="String" required="true"/>
    <h1 style="border:{!borderColor}">
        <apex:outputText value="{!myValue}"/>

So, you can use various section of Composition at various places but you will have to use whole component at given time.


apex:component are discrete standalone items, when we use a component it will fetch all component into a page where we used it.

But in apex:composition we have apex:define to define something into a composition and we can use it by apex:insert. So it will not include whole apex:composition but only provides body for the template's apex:insert components with matching apex:define component

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.