plonezopetemplate-talzpt

how to write python code in the zope page template


I am very new to Zope and Plone. I am trying to write python code in the index_html page. I have the list of objects of type person, now I want to reorder them. So, what I had before was:

<ul tal:define="persons python: context.portal_catalog(portal_type='Person');">
<tal:listing repeat="p persons">

Now I have this python code before the <tal:listing...

<?python
  order=[0,2,1]
  persons = [persons[i] for i in order]
?>

But somehow the order of the person remains the same. Also, I also don't like this way of writing python code in the view. Is there any way I could use this code for changing the order of the list?


Solution

  • Zope pagetemplates do not support a <? ?> syntax at all.

    However, you can loop over your python list in the tal:repeat just fine:

    <ul tal:define="persons python: context.portal_catalog(portal_type='Person');">
        <tal:listing repeat="i python:[0, 2, 1]">
            <li tal:define="p python:persons[i]" tal:content="p/name">Person name</li>
        </tal:listing>
    </ul>
    

    I suspect however, that you want to let the portal_catalog do the sorting instead, using the sort_on parameter (see the Plone KB article on the catalog):

    <ul tal:define="persons python: context.portal_catalog(portal_type='Person', sort_on='sortable_title');">
        <tal:listing repeat="p persons">
            <li tal:content="p/name">Person name</li>
        </tal:listing>
    </ul>
    

    If you want to do anything more complex, use a browser view to do the list massaging for you.