Ticket #3536 (closed Bug: fixed)
More flexible/reusable folder_contents and folder_listing
| Reported by: | alecm | Owned by: | |
|---|---|---|---|
| Priority: | minor | Milestone: | 2.1 |
| Component: | Templates/CSS | Keywords: | |
| Cc: |
Description
It would be nice to be able to reuse and customize the folder viewing templates more easily. They are very complex and making custom versions for any minor tweak (e.g., showing creation time instead of modification time, limiting the content types or workflow states displayed, etc.) requires duplicating the entire file and making changes which. This is not ideal, but fortunately ZPT offers enough flexibility to relsove this problem easily.
I've attached a patch which alters those templates and the scripts they depend on to allow for easy repurposing with minimal code duplication. The changes are as follows:
folder_contents: *added a macro ('contents') within main that includes the entire folder contents display, to allow for reuse in other templates. *added a macro ('add_button') for the content add button. This add button can be useful in many places, there's no need to force developers to rewrite it. Using the macro requires setting the 'contentTypes' and 'num_types' variables in any template that uses it. *added a slot ('listingheader') that allows templates using the 'contents' macro to override header defining what information is displayed for the objects (excludes title/id and the ordering field, which is displayed only if an explicit sort function is not defined (see below)). *added a slot ('item_display') which overrides the definition of which fields are displayed for each contained object (excludes title/id). *added an optional parameter 'contentFilter', which should be a mapping used to filter which items are retrieved by the folder's listFolderContents method. The folder classes support very useful filtering, it should be available to the templates. *added an optional parameter 'batch_size', which determines the size of folder_contents batching. Is there a reason to hardcode this at 100? *added optional parameter 'view_title', which alters the displayed title of the view. If the contentFilter is used, it is probably a good idea to have the page title reflect what sort of filtering is happening. *added an optional paremeter 'sort_func', which determines the sort order of the displayed results. With folders with large numbers of objects, the usual manual sorting features can be a hinderance. This function can either be a standard cmp function, or a an external list sorter (which is generally more efficient). If this is enabled the ordering column of the display table is disabled automatically.
folder_listing: *the optional parameters 'contentFilter', and 'sort_func' as defined in foldercontents. *an optional parameter 'limit_display', which is the maximum number of objects to display when doing a folder listing. This is useful because the listing doesn't support batching, and becomes very cumbersome when used with folders that contain many objects. *an optional parameter 'more_url', which is a url used to create a link where all the objects can be viewed when the number of objects exceeds 'limit_display' (defaults to 'folder_contents'). *a slot ('item_display') which is used to override the default display of each contained object.
the scripts: *added support for the above optional parameters ('sort_func', 'batch_size'). If 'sort_func' is defined, a test is performed to determine if it can take a single parameter (an empty list). If that test passes then the function is assumed to return a sorted list given an unsorted one. If the test fails the 'sort_func' is assumed to be a standard cmp type function.
Attachments
Change History
Changed 7 years ago by alecm
-
attachment
demo_folder_contents.4.pt
added
comment:1 Changed 7 years ago by alecm
In case that was all too abstract, I'm attaching a demonstration template which uses the macro in folder_contents and most of its features. The main reason I made these changes is that I got very tired of merging in changes to my folder_contents type templates whenever I updated plone.
The examle template has a batch_size of 25, shows only 'News Items', displays them sorted by creation date, and has a title which reflect the displayed content. The table of items uses the Creation date instead of the usual modification date, but could of course be used to display any relevant data about a particular piece of content. Hopefully you'll find this as potentially useful as I do. If anyone would like I can attach a template demonstrating uses for the folder_listing changes as well.
comment:2 Changed 7 years ago by limi
Interesting. Will look at this for our re-vamp of the folder_listing and folder_contents templates to use the catalog on the http://svn.plone.org/plone/CMFPlone/branches/uiteam-catalog-based-folder-listings branch - would you be interested in helping out?
comment:3 Changed 7 years ago by alecm
I'd be glad to help out. I've actually got a local version of folder_contents that uses the catalog. It works pretty well. It is slow for large folders that require batching in the display, because the catalog search results aren't cacheable. Also, it's slowed down by the fact that the standard Zope PathIndex doesn't offer a way to limit a catalog search to a particular folder (without a recursive search of its subfolders). Dieter's ManageableIndex does, but it's not fun generating a query for ManageableIndex through an html form.

demo_folder_contents.pt