3 min read

(For more resources on Plone, see here.)

Plone makes it easy to create new types of portlets that include custom programming logic for your site. There are several ways to create custom portlets, but the simplest way to get started is to use the add-on product collective.portlet.tal which provides a new type of portlet, called a TAL Portlet. This portlet allows you to write simple bits of code using Zope’s TAL templating language.

Let’s walk through a quick example of building a custom TAL portlet, which will show a randomly-selected news item from your site.

Installing collective.portlet.tal

Before you can add a TAL portlet, you must download the product from Plone.org/products and install the add-on product collective.portlet.tal on your site. The best way to do this is to modify your buildout.cfg file.

Add collective.portlet.tal to the eggs and zcml sections of your buildout. Here’s a code snippet with the changes made to it:

[buildout]   ...
eggs =
[instance] recipe = plone.recipe.zope2instance
zcml =

Once you’ve made these changes, re-run buildout by issuing the following command:

$ ./bin/buildout

Once you’ve added the product to your buildout, visit Site Setup and choose Add/Remove Products, to install collective.portlet.tal in your site.

Finally, add a few news items to your site so that we have something for our new TAL portlet to find.

Adding a simple TAL portlet

With the collective.portlet.tal product in place, the following can happen:

  1. Navigate to your Plone site.
  2. Choose Manage Portlets in the right column.
  3. From the Add portlet… drop-down list, choose TAL Portlet.

  4. You’ll see an empty text box in which you can enter a title. We will specify Featured News Item as our title. We’ll soon see the code needed to feature a random one of our site’s published news items.
  5. In addition to the Title text box, you’ll also see an HTML text area titled TAL code. Conveniently, this comes pre-populated with some boilerplate HTML and TAL code. Skim this, so that you get a feel for how this looks and what the common HTML structure is like, for a portlet in Plone.

As an immediate experiment, we will find the following snippet of code:

Body text

We will modify this, slightly, to:

Is this thing on?

Click on Save and navigate through the site, and you should see your first TAL portlet in action. Of course, there’s nothing in this example that couldn’t be accomplished with a static text portlet. So let’s navigate back to the Featured News Item portlet and make it a bit more interesting and dynamic.

Update the code in your TAL Portlet to include the following:

tal_define="newsitems python:context.portal_catalog
(portal_type='News Item', review_state='published');"

Featured News Item

tal_define="random_newsitem python:random.choice(newsitems)">
href="[replaced by random news item link]"
title="[replaced by random news item title]"
tal_attributes="href random_newsitem/getURL;
title random_newsitem/Title">[replaced by random news
item title]

More news...

Now, let’s go into more detail on a few of these sections, so that you understand what’s happening. If at any point you need more context, try reading the excellent ZPT reference manual at http://plone.org/documentation/tutorial/zpt.

Subscribe to the weekly Packt Hub newsletter. We'll send you the results of our AI Now Survey, featuring data and insights from across the tech landscape.

* indicates required