Override portlets or Subclassing

written on 2009-01-20

I found a great article on how to subclass the navigation portlet. Explanations and all. This entry is based on that article but doesn't have all the nice explanations.

(Source: http://transcyberia.info/archives/39-customizing-navigation-in-Plone3.html)

For a generic plone 3 portlet with a template portlet.pt and a module named portlet.py, this is what you need to do to subclass it. I tend to create a portlets folder in my porducts, which you have to register in the same way as the browser folder from your theme is registered. Don't forget the __init__.py and a configure.zcml!

Copy portlet.pt template from the source egg into your theme's portlets/ directory. Don't copy portlet.py, but instead create your own portlet.py. It needs only the following measly five lines of code:

from source.egg.portlet import Renderer
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile

class MyPortletRenderer(Renderer):
     render = ViewPageTemplateFile('portlet.pt')

In portlets/configure.zcml put the following lines:

<plone:portletRenderer
 portlet="source.egg.portlet.IPortletInterface"
 layer=".interfaces.IThemeSpecific"

 />

If, like in this example, the template in the renderer is called render (so render = ViewPageTemplateFile('portlet.pt')) and not index or _template or something the like, you just use the template instead of creating a class for it in a special portlet.py. The subclassing also works with multiple templates or differently named ones.

<plone:portletRenderer
   portlet="source.egg.portlet.IPortletInterface"
   template="portlet.pt"
   layer=".interfaces.IThemeSpecific"
   />

Pagination