You are here: Home Blog Topics plone.app.layout

plone.app.layout

Jan 26, 2010

Overriding the Title tag in Plone 3

by Mike Cullerton — last modified Jan 26, 2010 12:20 PM

The default title for a Plone 3 site has the page title on the left and portal title on the right, separated by an em dash. I needed to change that for a project. This how-to describes a process for overriding the page title of a buildout based Plone 3 site.

Quick Overview

You need to override the plone.htmlhead.title viewlet and create a class to render your title. The default version of the viewlet is defined in [eggs]/plone/app/layout/configure.zcml, and the class is defined in common.py. You'll need a few more things from common.py too. Override the viewlet in your own [theme]/browser/configure.zcml file, and add the location of your new class.

Getting Started

I needed to override a viewlet, but didn't know which one. A quick grep found a reference to the plone.htmlhead viewlet manager, and then one to the plone.htmlhead.title viewlet. Viewlets are found in the plone/app/layout/viewlets/configure.zcml file within the eggs area of your buildout. Here is the specific code in configure.zcml.

...
<browser:viewlet
name="plone.htmlhead.title"
manager=".interfaces.IHtmlHead"
class=".common.TitleViewlet"
permission="zope2.View"
/>
...

In this case, the plone.htmlhead.title viewlet references a class called .common.TitleViewlet. This means a file--in the same directory as configure.zcml, named common.py contains a class named TitleViewlet. Here is the TitleViewlet code from common.py.

class TitleViewlet(ViewletBase):

...

def index(self):
portal_title = safe_unicode(self.portal_title())
page_title = safe_unicode(self.page_title())
if page_title == portal_title:
return u"<title>%s</title>" % (escape(portal_title))
else:
return u"<title>%s &mdash; %s</title>" % (
escape(safe_unicode(page_title)),
escape(safe_unicode(portal_title)))

In the index method at the bottom, Plone sets the value of the <title> tag.

If the page title and the portal title are the same, the portal title is used. If not, both are used, with page title on the left and portal title on the right.

We needed to switch it, so that portal title was on the left, and page title was on the right.

What we did

To override the title viewlet, I created an entry in my theme's browser/configure.zcml file.

<!-- Our custom title viewlet --> 
<browser:viewlet
name="plone.htmlhead.title"
manager="plone.app.layout.viewlets.interfaces.IHtmlHead"
class=".myTitleViewlet.TitleViewlet"
layer=".interfaces.IThemeSpecific"
permission="zope2.View"
/>

Notice that there is a new layer statement, and that the manager line includes the complete path to IHtmlHead. The class statement now points to our new class containing code to override the title.

Here is the code from myTitleViewlet.py.

from zope.interface import implements
from zope.component import getMultiAdapter
from zope.viewlet.interfaces import IViewlet
from zope.deprecation.deprecation import deprecate

from Products.CMFPlone.utils import safe_unicode
from Products.Five.browser import BrowserView
from cgi import escape

from plone.app.layout.viewlets.common import ViewletBase

class TitleViewlet(ViewletBase):

...

def index(self):
portal_title = safe_unicode(self.portal_title())
page_title = safe_unicode(self.page_title())
if page_title == portal_title:
return u"<title>%s</title>" % (escape(portal_title))
else:
return u"<title>%s | %s</title>" % (
escape(safe_unicode(portal_title)),
escape(safe_unicode(page_title)))

I needed some of the imports from the top of common.py, and added one of my own for ViewletBase. The only code that really changed is the else clause, where the order of portal_title and page_title are switched, and the em dash is replaced by a pipe (|).

A great reference for overriding the <title> tag is HTML Head Title page which is part of the Plone Theme Reference.

 
 
Copyright © 2003-2017 Core Software Group | 303/809-1001 | Fort Collins, Colorado | All rights reserved.