Register a Custom Viewlet in Plone

How to register or add a custom viewlet in Plone using zopeskel and the plone3_theme option

Original article can be found here.

This registers a theme specific viewlet (not great if you're not actually making a whole theme) except in the future I should be able to expand on this article.

Go to your instance folder and then to the src directory.

cd zeoserver/src

Run the following command which will create a blank theme skeleton for us to edit:

../bin/zopeskel plone3_theme package.name

Where package.name is the name of what you will call your package.

Change directory into your newly created package (again of course replacing package and name for what you put in their place):

cd package.name/package/name

Get to the browser folder:

cd browser/

Edit the viewlets.py file and uncomment the class definition

joe viewlets.py

Use the viewlets.py information that exists already and follow that as example, but essentially it passes variables to your template file. Also remember to update the viewlet.pt part to a more fitting name and then rename that file (located in the same directory as viewlets.py)

EG:

from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from plone.app.layout.viewlets.common import ViewletBase

class MyViewlet(ViewletBase):
    render = ViewPageTemplateFile('viewlet.pt')
 
    def update(self):
        self.computed_value = 'any output'

Could be come:

from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from plone.app.layout.viewlets.common import ViewletBase

MYNAME = 'Dan'

class MyViewlet(ViewletBase):
    render = ViewPageTemplateFile('example_view.pt')
 
    def update(self):
        self.myname = MYNAME   

Obviously the script could be quite more complicated such as pulling information from the filesystem or getting the current logged in users username or something to that effect.

Now that you are editing your example_view.pt file, you could simply reference your Python variable like so:

<p id="example_viewlet"
tal:content="view/myname" />

This would obviously result in:

<p id="example_viewlet">Dan</p>

Edit your configure.zcml file located in that browser directory and copy out the "EXAMPLE (UNCOMMENT TO MAKE AVAILABLE)" declaration.

You may need to also make the necessary customisations to the file, such as updating the class line.

You can update the manager argument if a different viewlet manager will be parent to this viewlet. (important when using @@manage-viewlets).

Now that the Python and page template files are sorted, cd to the profiles/default directory:

cd ../profiles/default

Edit the viewlets.xml file and once again uncomment the "EXAMPLE (UNCOMMENT TO MAKE AVAILABLE)" line. There  is help information in this file which will help in deciding what existing viewlet to put  your before or after. /@@manage-viewlets will tell the viewlet names if you are unsure.

Add the necessaries to buildout eggs, zcml and develop sections and run a buildout.

Install the product on a Plone site and you will be almost good to go (it requires you use the entire skin) which obviously means an unthemed Plone site. This is a temporary place holding article anyway until I have made more progress with it.

Theme independent add-on

 If you would like the add-on to be theme independant (warning think this breaks the uninstallation procedure, or maybe it doesn't work for these at all).

Remove the following line from browser/configure.zcml:

layer=".interfaces.IThemeSpecific"