Rendering nested items with arbitrary depth using Chameleon ZPT

I am using the Pyramid framework and I would like to do with Chameleon an html menu with nested lists (ul, li) of arbitrary depth.

I cannot find any recursive method in Chameleon to do this. There seems to be such a general need, so I am wondering what is the correct way to render nested elements with arbitrary depth?

But maybe some widget menu already available, fully tested and compatible with Pyramid and Chamelon?

+3


source to share


2 answers


<ul metal:define-macro="comment_list">
  <li tal:repeat="comment comments" class="comment" comment_id="${comment.id}">
    <div>ID: ${comment.id} ${comment.body}</div>
    <div tal:define="comments comment.children">
      <ul metal:use-macro="template.macros['comment_list']" />
    </div>
  </li>
</ul>

      



+3


source


This is one answer to Graham's question about typing bismigalis' answer.

You start with an object Comment

like this:

class Comment():                                                                                     

    id = 0                                                                                           
    body = ""                                                                                        
    children = []                                                                                    

    def __init__(self, id, body, children):                                                          
        self.id = id                                                                                 
        self.body = body                                                                             
        self.children = children

      

Then you will create a list of comments and their children. For the sake of play, I just did it by hand (sorry if it wasn't formatted correctly):

comments = []
comment1 = Comment(1, "First comment", None)
comment2 = Comment(2, "Second comment", [
        Comment(3, "Third comment", [
                Comment(5, "Fifth comment", None)
            ]
        ),
        Comment(4, "Fourth comment", None),
    ]
)

comment6 = Comment(6, "Sixth comment", None)

comments.append(comment1)
comments.append(comment2)
comments.append(comment6)

      



Then you just make it part of the returned dictionary from your code like:

return {'comments': comments}

      

The template code in bismagalis' answer will generate the following HTML:

        <ul>
          <li class="comment" comment_id="1">
            <div>ID: 1 First comment</div>
            <div>
              <ul>

        </ul>
            </div>
          </li>
          <li class="comment" comment_id="2">
            <div>ID: 2 Second comment</div>
            <div>
              <ul>
          <li class="comment" comment_id="3">
            <div>ID: 3 Third comment</div>
            <div>
              <ul>
          <li class="comment" comment_id="5">
            <div>ID: 5 Fifth comment</div>
            <div>
              <ul>

        </ul>
            </div>
          </li>
        </ul>
            </div>
          </li>
          <li class="comment" comment_id="4">
            <div>ID: 4 Fourth comment</div>
            <div>
              <ul>

        </ul>
            </div>
          </li>
        </ul>
            </div>
          </li>
          <li class="comment" comment_id="6">
            <div>ID: 6 Sixth comment</div>
            <div>
              <ul>

        </ul>
            </div>
          </li>
        </ul>

      

It looks like there are quite a few extraneous tags in there <div>

and <ul>

so I may have missed something ...

+1


source







All Articles