pythonpython-3.xpyramidchameleontemplate-tal

How can I make a "FOR"(loop) in html, using chameleon and pyramid in python 3.4?


How can a make a loop using chameleon and pyramid in my html? I search but i found nothing like that =/ Is easier use javascript in this case? I use datatable in MACADMIN(bootstrap theme).

<div class="table-responsive">
  <table cellpadding="0" cellspacing="0" border="0" id="data-table" width="100%">
    <thead>
      <tr>
        <th>
          Rendering engine
        </th>
        <th>
          Browser
        </th>
        <th>
          Platform(s)
        </th>
        <th>
          Engine version
        </th>
        <th>
          CSS grade
        </th>
      </tr>
    </thead>
    <tbody>
         Maybe put FOR here? like {for x items in "TABLE"}
      <tr>
        <td>
          {orgao_doc[x].nome}
        </td>
        <td>
          {orgao_doc[x].cargo}
        </td>
        <td>
          {orgao_doc[x].coleta}
        </td>
        <td>
          {orgao_doc[x].email}
        </td>
        <td>
          {orgao_doc[x].endereco}
        </td>
      </tr>
    </tbody>
  </table>
  <div class="clearfix">
  </div>
</div>

Solution

  • Use a tal:repeat attribute to repeat parts of a template, given a sequence:

    <tbody>
      <tr tal:repeat="item orgao_doc">
        <td>${item.nome}</td>
        <td>${item.cargo}</td>
        <td>${item.coleta}</td>
        <td>${item.email}</td>
        <td>${item.endereco}</td>
      </tr>
    </tbody>
    

    The <tr> tag is repeatedly inserted into the output, once for each element in orgao_doc. The name item is bound to each element when rendering this part of the template.