2012/12/18

Plone Template Global Variables

It is a common task to get information about the state of your site and other global variables. For Plone 4+, the recommended approach is to use one of the following browser views:

  • @@plone_portal_state
  • @@plone_context_state
  • @@plone_tools

You can find how they are defined in plone.app.layout/globals/configure.zcml:

<browser:page
   name="plone_tools"
   for="*"
   permission="zope.Public"
   class=".tools.Tools"
   allowed_interface=".interfaces.ITools"
   />

<browser:page
   name="plone_context_state"
   for="*"
   permission="zope.Public"
   class=".context.ContextState"
   allowed_interface=".interfaces.IContextState"
   />

<browser:page
   name="plone_portal_state"
   for="*"
   permission="zope.Public"
   class=".portal.PortalState"
   allowed_interface=".interfaces.IPortalState"
   />

For example, a straight way to add the homepage link:

<a href="http://www.yoursite.com/">home</a>

This works only for the site running on the specific domain name. When testing, you are likely to run on localhost or IP address, that the homepage URL needs updated accordingly. The recommended style looks like this:

<a href="#"
   tal:define="home_url context/@@plone_portal_state/portal_url"
   tal:attributes="href home_url">home</a>

No comments: