Folder Listing as a Plone Portlet Mini-HOWTO
How to create a box that displays the contents of a folder using Zope page templates and the Plone 'portlet' macro package.
This folder listing portlet displays the title of the folder, its
description, and a list of each item in the folder. Mousing over each
link pops up its description in browsers that understand the
title anchor (A) tag attribute.
- Log into Plone using an account that has the Manager role.
- From the Plone Control Panel (the
Plone Setuplink underneath the search box), access the Zope Management Interface. - Click on
portal_skins. - Click on
custom. - From the Select type to add drop-down box,
choose
Page Templateand clickAdd. - When prompted, set the ID of the new page template to
portlet_folder_contentsand clickAdd and Edit. - Paste the following code into the text area presented on the
Edit screen, then click
Save Changes:<html xmlns:tal="http://xml.zope.org/namespaces/tal" xmlns:metal="http://xml.zope.org/namespaces/metal" i18n:domain="plone"> <body> <!-- A folder's contents as a Box --> <div metal:define-macro="portlet" tal:omit-tag=""> <div class="portlet" id="portlet-folder-contents"> <h5 tal:content="here/title_or_id">Folder Name</h5> <div class="portletBody"> <div class="portletContent odd" tal:content="here/description"> Folder description </div> <div tal:repeat="item here/objectValues" tal:omit-tag=""> <div class="portletContent odd" tal:condition="repeat/item/odd"> <a href="#odd" title="Odd item description" tal:content="item/title_or_id" tal:attributes="href item/absolute_url; title item/description">Odd item title</a> </div> <div class="portletContent even" tal:condition="repeat/item/even"> <a href="#even" title="Even item description" tal:content="item/title_or_id" tal:attributes="href item/absolute_url; title item/description">Even item title</a> </div> </div> <!-- tal:repeat="item here/objectValues" --> </div> <!-- class="portletBody" --> </div> <!-- class="portlet" --> </div> <!-- metal:define-macro="portlet" --> </body> </html> - Test the portlet by clicking
Test. This should return a listing of the portal_skins/custom folder. - Use the portlet by adding it to the portal's
left_slots or right_slots
property. For example, to display the contents of the Plone
Members folder, add
portal/Members/portlet_folder_contents. - To create a version of the portlet that only displays the folder
contents box if a user has logged in, change the lines containing
<div class="portlet" id="portlet-folder-contents">to<div class="portlet" id="portlet-folder-contents" tal:define="name user/getUserName" tal:condition="python:test(name=='Anonymous User',False,True)"> - Note that if access to the folder itself is restricted,
executing the portlet in the fashion described above will cause Zope
to redirect unauthenticated web site accesses to the login page
prior to the execution of the Anonymous User test. Because the
login page also renders the boxes listed in
left_slots and right_slots, Zope
will redirect the user to a HTTP basic authentication prompt in
order to avoid an infinite loop. This means that any
unauthenticated portal access will prompt for HTTP basic
authentication.
To avoid this situation, make a copy of the portlet and change all references to the TAL keyword
hereto the explicit path of the folder object. For example, the header element<h5 tal:content="here/title_or_id">Folder Name</h5>
becomes<h5 tal:content="root/portal-id/path/title_or_id">Folder Name</h5>
(whereportal-id/pathis the URI of the folder from the Zope root, not from the portal root). Add this customized portlet to the portal's left_slots or right_slots property using the standard syntaxhere/copy_of_portlet_folder_contents/macros/portlet(wherecopy_of_portlet_folder_contentsis the ID of the customized portlet).Yes. If you are new to Zope, page templates, and the Zope security model, this is very confusing.