[SalesForce] Controllers with apex:include and apex:composition

When you use <apex:include> or <apex:composition>, what happens for controllers?

Does each of those apex:pages need its own controller?

apex:include

<apex:page title="Outer Page" controller="OuterPageController">
    <apex:include page="InnerPage"/>
</apex:page>

<apex:page title="Inner Page" controller="InnerPageController">
    1. Can I use {!properties} from OuterPageController?
    2. If both pages use the same controller type, is the same
        instance used, or are there two instances?
</page>

apex:composition

<!-- Page: composition -->
<apex:page>
    <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"/>

    3. Can I use {!properties} from OuterPageController?
    4. If both pages use the same controller type, is the same
        instance used, or are there two instances?
    5. Can I test or examine the contents of an <apex:insert> in my inner controller?

</apex:page>

<apex:page controller="outerPageController">
    <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>
    </apex:composition>
</apex:page>

Best Answer

If you can afford to put the markup in the entry point page, this may be interesting:

  • apex:include targets cannot traverse standard controllers
  • but apex:composition defines can traverse included pages

Otherwise I think the only way Visualforce can talk to other Visualforce is by using an apex:component that exposes an apex:attribute.

With both apex:include and apex:composition the 'inners' are evaluated and flattened before they get inserted.

re: includes:

Can I use {!properties} from OuterPageController?

Nope.

re: compositions:

Can I use {!properties} from OuterPageController?

Also nope, those properties can only be evaluated from inside the apex:define block.

Can I test or examine the contents of an in my inner controller?

Not from the inner controller :-( there's no way to get a handle on the value of an <apex:insert /> as a variable or property.

Related Topic