Tell me more ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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>
share|improve this question

2 Answers

up vote 2 down vote accepted

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:

1. Can I use {!properties} from OuterPageController?

Nope.

2. If both pages use the same controller type, is the same instance used, or are there two instances?

Two instances.

re: compositions:

3. Can I use {!properties} from OuterPageController?

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

4. If both pages use the same controller type, is the same instance used, or are there two instances?

Two instances.

5. 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.

share|improve this answer
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 at 22:13

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.

share|improve this answer
Thanks. I've clarified my questions a bit above. – Benj Jan 23 at 18:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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