PloneBatch usage

by Helge Tesdal last modified Apr 01, 2009 06:29 AM
PloneBatch is an extension of the regular Batch class in ZTUtils. It also comes with a handy macro for making the navigation menu to ease development. It's perfect for those 2000 items search result pages.
The main steps of including the PloneBatch in your template will be to import the Batch, make a batch from your resultset, use the navigation macro and iterate over the batch elements. Source code in this example comes from the search.pt template in Plone. First step, set up your Batch In the beginning of your template you would import Batch from Plone and define some variables used by it::
'results' is the list to make batch from. In this case it is the results of a search (catalog query), but it can be any list. 'Batch' is imported from 'Products.CMFPlone' and made available in the template as 'Batch'. 'b_size' is the batch size, the number of elements to display in each batch. 'b_start' is the first element in the batch. It is retrieved from the request, or 0 if not found. Usually you will see 'b_start' set to different values in the URL when you navigate around the pages in the result. Second step, make a batch from the results After setting up 'Batch' and other variables, we are ready to make the batch from our result::
If the next batch would contain no more than 'orphan' elements, it is combined with the current batch. In this case, if the next batch would contain only 1 element, it is combined with the current page. Third step, make a navigation menu For your convenience we have provided a macro to make the navigation menus. This macro will typically be called before and after iterating over the items in the batch::
The result will be something similar to this:: «Previous 30 1 2 3 4 [5] 6 7 8 ...Last Next 30» Fourth step, iterate over the batch Now you are ready to iterate over the batch. This is done exactly as you would iterate over the regular result set::

The URL from result will be written here

You might want to call the navigation macro again after iterating over the result to get a navigation menu both above and below the result.