Find your content:

Search form

You are here Sites Page to template to component relationship


I am looking for the best way to set dynamic attributes for a component that is included on a 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"
    <apex:insert name="header">
            ActiveTab="{!ActiveTab}" AllowSearchLink="{!CanSearch}" />

    <apex:insert name="body" />

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


Home Page (Visualforce Page)

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

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
    <apex:outputPanel rendered="{!IsAuthenticatedUser}" layout="none">
        <apex:outputPanel layout="none" rendered="{!AllowSearchLink}">
            <li class='{!IF(ActiveTab="Search", "active", "")}'><a
        <li class='{!IF(ActiveTab="Info", "active", "")}'><a
    <li class='{!IF(ActiveTab="ContactUs", "active", "")}'><a

Attribution to: Brad Ullery

Possible Suggestion/Solution #1

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 .

Attribution to: Mohith Shrivastava

Possible Suggestion/Solution #2

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"/>

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 layout="none" rendered="{!imgover=='true'}">
   <!-- render the content that replaces the image -->

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

Attribution to: Bob Buzzard
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content