Recipes by Category

App Distribution (2) Bundle logic, interface and services for distribution. App Logic (37) The Apex programming language, workflow and formulas for logic. Collaboration (6) The Salesforce Chatter collaboration platform. Database (29) Data persistence, reporting and analytics. Integration (33) Web Service APIs and toolkits for integration. Security (9) Platform, application and data security. Tools (4) Force.com tooling User Interface (36) Visualforce MVC and metadata-drive user interfaces. Web Sites (12) Public web sites and apps with optional user registration and login.
Beta Feedback
Cookbook Home » Using Force.com Site-Specific Merge Fields

Using Force.com Site-Specific Merge Fields

Post by Developer Force  (2010-07-16)

Status: Certified
Level: novice

Problem

You want to create one Visualforce “About Us” page to be used for several public sites, so you can't hard-code site-specific values like site URL, site email address, and site template. You want the “About Us” page to conditionally display links for registration and login, and connect the “Contact Us” link to each respective site's designated email address.

Solution

Take advantage of the available merge fields when creating the Visualforce pages used for your public sites. By using merge fields, the same Visualforce page can be used for multiple sites.

The following merge fields are available:

Merge Field Description
{!$Site.Name} Returns the API name of the current site.
{!$Site.Domain} Returns the Force.com domain name for your organization.
{!$Site.CustomWebAddress} Returns the value of the Custom Web Address field for the current site.
{!$Site.OriginalUrl} Returns the original URL for this page if it is a designated error page for the site; otherwise, returns null.
{!$Site.CurrentSiteUrl} Returns the value of the site URL for the current request (for example, http://myco.com/ or https://myco.force.com/prefix/).
{!$Site.LoginEnabled} Returns true if the current site is associated with an active login-enabled portal; otherwise returns false.
{!$Site.RegistrationEnabled} Returns true if the current site is associated with an active self-regitration-enabled Customer Portal; otherwise returns false.
{!$Site.IsPasswordExpired} For authenticated users, returns true if the currently logged-in user's password is expired. For non-authenticated users, returns false.
{!$Site.AdminEmailAddress} Returns the value of the Site Contact field for the current site.
{!$Site.Prefix} Returns the URL path prefix of the current site. For example, if your site URL is myco.force.com/partners, partners is the path prefix. Returns null if the prefix is not defined, or if the page was accessed using a custom Web address.
{!$Site.Template} Returns the template name associated with the current site; returns the default template if no template has been designated.
{!$Site.ErrorMessage} Returns an error message for the current page if it is a designated error page for the site and an error exists; otherwise, returns an empty string.
{!$Site.ErrorDescription} Returns the error description for the current page if it is a designated error page for the site and an error exists; otherwise, returns an empty string.
{!$Site.AnalyticsTrackingCode} The tracking code associated with your site. This code can be used by services like Google Analytics to track page request data for your site.

In the following example, the Visualforce page uses the{!$Site.Template}, {!$Site.AdminEmailAddress}, {!$Site.LoginEnabled}, and {!$Site.RegistrationEnabled} merge fields (shown in bold).

<apex:page showHeader="false" title="About Us">
<!-- This page uses the template associated with the site. 
     No need to hardcode the template name. -->  
    
  <apex:composition template="{!$Site.Template}">
    <apex:define name="body">
      <apex:form>
        <apex:outputPanel layout="block">
          <apex:panelGrid columns="1">
            <apex:outputText value="About Us text goes here"/>
            <apex:outputText value="..."/>
            <apex:outputText value="..."/>

<!-- The contact email address is dynamically populated with the 
     current site's contact email address. -->  
    
            <apex:panelGroup id="Contact">
              <apex:outputText value="Contact us by email at:"/>
              <apex:outputLink value=
                  "mailto:{!$Site.AdminEmailAddress}">
                  company@acme.com</apex:outputLink>
            </apex:panelGroup>

<!-- The Login Panel group is rendered if the current site 
     is login-enabled. -->  
    
            <apex:panelGroup id="Login" rendered=
                "{!$Site.LoginEnabled}">
              <apex:outputText value="Existing User? "/>
              <apex:outputLink value="{!$Page.SiteLogin}"> Login to 
                  Acess your Account</apex:outputLink>
            </apex:panelGroup> <apex:outputText value="..."/>
              <apex:outputText value="..."/>
              <apex:outputText value="..."/>

<!-- The Register Panel group is rendered if the Customer Portal 
     associated with the current site is registration-enabled. -->  
    
            <apex:panelGroup id="Register" rendered=
                "{!$Site.RegistrationEnabled}">
              <apex:outputText value="New User?"/>
              <apex:outputLink value="{!$Page.SiteRegister}">
                  Register to get an Account</apex:outputLink>
            </apex:panelGroup>
          </apex:panelGrid>
        </apex:outputPanel>
      </apex:form>
    </apex:define>
  </apex:composition>
</apex:page>

Discussion

Sites takes advantage of the power and flexibility of Visualforce. With the out-of-box merge fields and additional expressions available, you can more easily create feature-rich site pages across multiple sites.

Share

Recipe Activity - Please Log in to write a comment

Note to self: 
  These Sites global variables are only available if my context is a visualforce Site, if i create a visualforce page and access it from my normal user login these variables will be empty.

by ronhess.0  (2010-09-16)

X

Vote to Verify a Recipe

Verifying a recipe is a way to give feedback to others and broaden your own understanding of the capabilities on Force.com. When you verify a recipe, please make sure the code runs, and the functionality solves the articulated problem as expected.

Please make sure:
  • All the necessary pieces are mentioned
  • You have tested the recipe in practice
  • Have sent any suggestions for improvements to the author

Please Log in to verify a recipe

You have voted to verify this recipe.