0

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?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Sadiksha Gautam
  • 5,032
  • 6
  • 40
  • 71
  • The ` ?>` syntax is not supported at all by Zope page templates. Where did you find that syntax? – Martijn Pieters Jul 25 '12 at 13:44
  • I found it from www.mail-archive.com/zpt@zope.org/msg00046.html . I realized that if i change the persons to new_persons in python code and try to access it in , it doesn't know about this new_person! I think I should write this python code somewhere else. Currently I am writing it in the browser template! Where should I write this code and how do I access this code on the template? – Sadiksha Gautam Jul 25 '12 at 13:47
  • 1
    Ah, that is someone *asking* for a feature, not describing what ZPT can do. :-) – Martijn Pieters Jul 25 '12 at 13:49
  • but is there anyway i can change the order of the list person? – Sadiksha Gautam Jul 25 '12 at 13:50
  • I've already posted an answer below. – Martijn Pieters Jul 25 '12 at 13:51

1 Answers1

4

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343