Blog Entry 12 years, 8 months ago

Batching in custom template

This is the simple guide for creating a terribly simple config of control panel of Plone 4, most of the note is taken from here (http://plone.org/documentation/kb/how-to-create-a-plone-control-panel-with-plone.app.registry). Assume we already have a Plone 4 package, called pure.theme

When having much of items to display, like inside a news category, we should break it into pages. It looks somehow like this:

Plone navigation screenshot

Plone has a Batch class which supports above feature, staying at Products.CMFPlone.Batch. If you utilize (inherited) some existing folder views, you could already have it (the batch & navigation) present inside the template. If not, we could put that in manually. Here are the steps:

1. Inside the template, declare the Batch class:

<tal:misc define="Batch python:modules['Products.CFMPlone'].Batch">

2. Determine the list or method returning list of items. Here in this example is getting all Documents via portal catalog:

tal:define="all_items python:context.portal_catalog(portal_type='Document', review_state='published');"

3. Set some variables, and creating batch object:

tal:define="b_start python:request.get('b_start', 0);
            per_page python:request.get('per_page', 5);
            batch python:isinstance(all_items, Batch) and items or Batch(items, per_page, int(b_start), orphan=1);"

From here, you could use the batch as the list of items for displaying

<div tal:repeat="item batch">
    <div class="item-title" tal:content="item/pretty_title_or_id | nothing" />
    <div class="item-description" tal:content="item/Description | nothing" />
</div>

4. That is not complete yet, we still need the navigation which displays the pages, next, previous links,... This one is extremely simple, just using this macro (put it anywhere you want, anywhere == $places-inside-step3-scope):

<div metal:use-macro="context/batch_macros/macros/navigation" />

 

Recent Reads