Search code examples
pythonpyramidchameleontemplate-tal

Pyramid and Chameleon ZPT Repetition


I am trying to loop through a list and get an output like this:

Python:

items=['item1', 'item2', 'item3']
return dict(items=items)

HTML:

<ul>
<li><a href="/item1>item1</a></li>
<li><a href="/item1>item2</a></li>
<li><a href="/item1>item3</a></li>
</ul>

I can get the list part right but adding it to the anchor is not going so well.


Solution

  • How about (supposing 'items' is a namespace passed from your code to your template):

    <ul>
     <tal:block repeat="item items">
      <li><a href="" tal:attributes="href item" tal:content="item">item</a></li>
     </tal:block>
    </ul>
    

    You can put tal:repeat on the li element, but I personally like to use a dedicated tag using an element in the tal namespace (idiomatic choice is tal:block).

    Also see: http://drdobbs.com/web-development/184404974