def traverse(tree, depth=1): ul = htmlutils.Tag('ul', classes={ 'nav', 'nav-pills', 'nav-stacked', 'depth-' + str(depth), }) yield ul.start() for page, children in tree: is_active = page.pk == calling_page.pk li = htmlutils.Tag('li') li.toggle_class('active', is_active) yield li.start() if is_active: yield htmlutils.tag('a', page.title, href='#') else: yield htmlutils.tag('a', page.title, href=page.url) if children: yield from traverse(children, depth=depth + 1) yield li.end() yield ul.end()
def test_tag_helper(self): tag = htmlutils.tag('a', 'link', href='#') self.assertEqual(tag, '<a href="#">link</a>')