pythonmongodbpyramidchameleon

Pyramid / mongo : building a nested tal:repeat for a EmbeddedDocumentListField


i'm building a little app and testing at the same time Pyramid/Chameleon + MongoDB So far, I LOVE it but I've encountered a dead end

Quick look a the data I want to display from a category collection

_id:"category"
themes:Array
  0:Object
    _id:"theme1"
  1:Object
    _id:"theme2"
  2:Object
    _id:"theme3"
user:"username"

Themes are an EmbeddedDocumentListField in mongoengine (but for this I only need the id)

I worked out how to tal:repeat my categories with

pt file

<div class = "category" tal:repeat="c categories">
  <h2>${c.id}</h2>
</div>

viewmodel

self.categories = get_category_for_user(user)

What I want now is to nest tal:repeat to display a result like this

<div class = "category" tal:repeat="c categories">
  <h2>${c.id}</h2>
  <div class="theme" tal:repeat="t themes">
    <div class="title">
      <a href="/theme/${t.id}">${t.id}</a></div>
    </div>
  </div>
</div>

The thing is to get themes, i need the category, and I haven't figured how to extract the category used in the loop to the viewmodel. Is there any way to pass variables from the pt to the viewmodel py file? Something like tal:repeat t themes(${c.id}) ? Or am I doing this completly wrong and there is a simple way to do that?


Solution

  • I found my answer in a previous question here, sorry for disturbance

    tal nested dictionary syntax

    The answer was VERY simple :

    <div class = "category" tal:repeat="c categories">
      <h2>${c.id}</h2>
      <div class="theme" tal:repeat="t c.themes">
        <div class="title">
          <a href="/theme/${t.id}">${t.id}</a></div>
        </div>
      </div>
    </div>
    

    Diffence lies on line 3 with reuse of the c variable to get the nested data.

    Pyramid is awesome.