6

I am looking for the best way to set dynamic attributes for a component that is included on a Force.com site template page, but varies from page to page.

For example, let's say I have a Home Page, a Search Page, an Info Page and a Contact Us Page. In my header component (Visualforce Component) I have an attribute to specify the "active tab" so that I can set the class to "active".

Since my component is defined on the template page rather than the actual content page, I am curious to know how I may set that value?

Am I approaching this in the right manner, or is there a better way to accomplish my task?

I have listed sample Visualforce code below to demonstrate my issue.

Template (Visualforce Page)

<apex:page showHeader="false"
    controller="TemplateController">
    <apex:insert name="header">
        <c:Header
            IsAuthenticatedUser="{!NOT(ISPICKVAL($User.UserType,'Guest'))}"
            ActiveTab="{!ActiveTab}" AllowSearchLink="{!CanSearch}" />
    </apex:insert>

    <apex:insert name="body" />

    <apex:insert name="footer">
        <c:Footer />
        <site:googleAnalyticsTracking />
    </apex:insert>

</apex:page>

Home Page (Visualforce Page)

<apex:page showHeader="false" title="{!$Label.SiteTitle}" 
    cache="false" contoller="HomeController">
    <apex:composition template="{!$Site.Template}">
        <apex:define name="body">  
             <h1>
                 {!$Label.SiteTitle}
             </h1>
        </apex:define>
    </apex:composition>
</apex:page>

Header (Visualforce Component)

<apex:component id="headerComponent">
<apex:attribute name="IsAuthenticatedUser" type="boolean"
    description="" default="false" />
<apex:attribute name="AllowSearchLink" type="boolean" description=""
    default="false" />
<apex:attribute name="ActiveTab" type="string" description=""
    default="" />
<ul id="nav">
    <li class='{!IF(ActiveTab="Home", "active", "")}'><a
        href="{!URLFOR($Page.HomePage)}"><span>{!$Label.Home}</span></a></li>
    <apex:outputPanel rendered="{!IsAuthenticatedUser}" layout="none">
        <apex:outputPanel layout="none" rendered="{!AllowSearchLink}">
            <li class='{!IF(ActiveTab="Search", "active", "")}'><a
                href="{!URLFOR($Page.SearchPage)}"><span>{!$Label.SearchLink}</span></a></li>
        </apex:outputPanel>
        <li class='{!IF(ActiveTab="Info", "active", "")}'><a
            href="{!URLFOR($Page.InfoPage)}"><span>{!$Label.Info}</span></a></li>
    </apex:outputPanel>
    <li class='{!IF(ActiveTab="ContactUs", "active", "")}'><a
        href="{!URLFOR($Page.ContactUs)}"><span>{!$Label.ContactUs}</span></a></li>
</ul>

3
  • Maybe this could help ? wiki.developerforce.com/page/Controller_Component_Communication Commented Nov 9, 2012 at 14:59
  • @Sdry That link looks very promising. Thanks, I will give it a try. Commented Nov 14, 2012 at 15:24
  • @Sdry I thought that was what I was looking for, but it leaves out the Template controller. This was close, but rather than a Page Controller/Component Controller relationship, I want a Page Controller/Template Controller relationship. Commented Nov 15, 2012 at 21:27

2 Answers 2

4

The way that I've handled this in the past is to use the apex:variable component in conjunction with the composition template.

For example, I have a site where every page needs to display the same image except for the home page.

In my home page, I define some content for the template that sets up a variable:

<apex:define name="imageoverride">
    <apex:variable var="imgover" value="true"/>
</apex:define>

and then in my templated page I have:

<apex:variable var="imgover" value="false"/>
<apex:insert name="imageoverride" />

which means that after this point, for every page aside from the home page, the imgover variable will contain the value 'false', but for the home page it will have the value 'true'. I can then inspect this and take appropriate action:

<apex:outputPanel layout="none" rendered="{!imgover=='false'}">
   <!-- render the standard image -->
</apex:outputPanel>

<apex:outputPanel layout="none" rendered="{!imgover=='true'}">
   <!-- render the content that replaces the image -->
</apex:outputPanel>

In your case I think it should just be a matter of each page having an apex:define that sets a variable of activeTab to the appropriate value ('Home', 'Contact' etc).

1
  • Excellent. That did the trick! Commented Nov 19, 2012 at 15:32
0

You can write an intermediate component between Home Page and Template Page .This will provide us the ability to pass the value from Home Page to the Template Page and then you can pass on that as an attribute to your header page .

3
  • I am not sure I follow. Are you suggesting that the component be added to the page rather than the template? Commented Nov 15, 2012 at 21:26
  • Nope i am suggesting lets have one more component that will provide us the ability to pass values from one page to other.Sorry i know its kind of difficult to understand Commented Nov 15, 2012 at 21:30
  • I still don't follow. Could you provide an example by referring to the sample pages/components I provided? Or would that structure and relationship be entirely different? Commented Nov 15, 2012 at 21:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .