10

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>
14

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.

  • 1
    That appears correct to me. Think of each include as being something which is being instantiated, evaluated and then parsed into the parent. There is not a tight interaction, one is just waiting for a result. – joshbirk Jan 23 '13 at 22:13
  • 1
    Are you sure apex:includes don't share the same instance of the controller? I thought I tested it at one point and they did. – Phil B Sep 20 '13 at 16:43
  • 3
    I just setup a small test and it appears they do indeed share the same instance of the controller in an apex:include. Could you please test on your end to confirm this is correct? This thread seems to come to the same conculsion unless I'm missing something: boards.developerforce.com/t5/forums/forumtopicprintpage/… – Phil B Sep 20 '13 at 19:29
  • 1
    I think both official examples (composition developer.salesforce.com/docs/atlas.en-us.206.0.pages.meta/… and include developer.salesforce.com/docs/atlas.en-us.206.0.pages.meta/…) do have an example where the pages share their controller. So this answer is probably incorrect these days! – Willem Mulder May 31 '17 at 8:44
2

Composition refers to a VF page which can be used as a template, it assumed it will have the insert component, which refer back to the page's define components.

Include is similar in function, but is really more for when you aren't trying to design a template. You just need to pull that VF page into another.

The different pages do not require a controller, but they can. The context is similar to embedding custom components.

1

As for two instances there is exclusion. If page and template use the same controller then there is only one instance created of this controller. You can try this if you use example from salesforce documentation http://www.salesforce.com/us/developer/docs/pages.

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.