Задача шаблона хамелеон / TAL / ZPT в тал: повторить - PullRequest
0 голосов
/ 25 июня 2019

Я пытаюсь использовать Chameleon Template для Python (он основан на языке Zope TAL / METAL).Этот простой шаблон вызывает ошибку (dic словарь в списке):

    <tr tal:repeat="dic myitems">
        <span tal:omit-tag="" tal:repeat="pair dic.items()">
            <td tal:content="pair[1 if dic.index else 0]"></td>
        </span>
    </tr>
AttributeError: 'dict' object has no attribute 'index'

 - Expression: "pair[1 if dic.index else 0]"
 - Filename:   <string>
 - Location:   (line 21: col 33)
 - Arguments:  repeat: {...} (0)
               dic: {...} (11)
               with_nginx_distribution: <list - at 0x7f30b34b20e0>
               pair: <tuple - at 0x7f30b36c7b90>
               css: \n.tabs {\n  position: relative;   \n  min-height: 20...
               target_language: <NoneType - at 0x7f30b46ade00>

Я пытался dic.index, dic.item.index, repeat.index, repeat.item.index, но все они выглядятошибочно.Каков правильный синтаксис для доступа к индексу циклического элемента?

1 Ответ

0 голосов
/ 25 июня 2019

Документация устарела, правильное выражение repeat['variable'].index как здесь:

from chameleon import PageTemplate

tmpl = '''
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:tal="http://xml.zope.org/namespaces/tal"
      xmlns:metal="http://xml.zope.org/namespaces/metal"
      xmlns:i18n="http://xml.zope.org/namespaces/i18n">

<table border="1">
<tr tal:repeat="d xxx">
<td tal:repeat="p d.items()">
<span tal:omit-tag="" tal:content="p[1 if repeat['d'].index else 0]"></span>
</td>
</tr>
</table>

</html>
'''

template = PageTemplate(tmpl)
data = {"xxx": [
    {"name": "John", "age": 30},
    {"name": "Michael", "age": 34}
]}
print(template(**data))
...