Question

I tried to create template using chameleon. Here is a code snipet.

Calling module runtemp.py:

delete_list=[]
delete={'Name':'aaa','Sirname':'bbb','Friends':['ccc','ddd','eee']}
delete_list.append(delete)
templates = PageTemplateLoader(os.path.join(path, "templates"))
template = templates["delete_user.pt"]
print template(tdelete_list=delete_list)

Template file delete_list.pt:

 <?xml version="1.0" encoding="UTF-8"?>
 <Delete>
   <DeleteRequest>

       <DeleteItems tal:repeat="deletions tdelete_list">

           <Deleteuser tal:repeat="delete repeat.deletions" >

                <Name tal:content="repeat.delete.Name"></Name>
                <Sirname tal:content="repeat.delete.Sirname"></Sirname>
                 <Friends>
                      <Friend tal:repeat="friend repeat.delete.Friends">
                               <Value tal:content="friend"></Value>
                       </Friend>
                 </Friends>

           </Deleteuser>

     <DeleteItems>

    </DeleteRequest>

 </Delete>

Output i got:

 <Delete>
        <DeleteRequest>

           <DeleteItems>



           </DeleteItems>
  </DeleteRequest>
 </Delete>

My problem is the middle tags are not getting printed; what is wrong?

Was it helpful?

Solution

The line <DeleteItems tal:repeat="deletions tdelete_list"> means to loop over tdelete_list and put each element in the variable deletions.

Thus, your inner loop just needs to loop over deletions; the repeat. prefix is not used here:

<Deleteuser tal:repeat="delete deletions" >

    <Name tal:content="delete.Name"></Name>
    <Sirname tal:content="delete.Sirname"></Sirname>
     <Friends>
          <Friend tal:repeat="friend delete.Friends">
                   <Value tal:content="friend"></Value>
           </Friend>
     </Friends>

</Deleteuser>

The repeat.deletions variable is actually only used to store loop metadata; the current count, the first and last flags, the odd and even flags, etc.

OTHER TIPS

You're iterating over tdelete_list which is a list with one element (the dict you create in line 2). That's how you get that one <DeleteItems /> tag. Within that tag, you try to iterate over repeat.deletions, however in the context you pass to the template, there's no object named repeat.deletions.

I guess from your variable naming that you're misinterpreting what tal:repeat="deletions tdelete_list" does - it iterates over tdelete_list and assigns the name deletions to each element of that iterable in turn. Inside the <DeleteItems /> tag, you can access that element under that name.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top