Question

I've made a template for representing a tree structure. Each node of the tree has an id, a name, a list of children (tree_children) and an expanded property.

I arranged a few nodes in a tree structure and then called the following function with the root node:

def print_tree_info(oCat, iOffset=0):
    """
    just for testing purposes. print to console
    """
    sOffset = '   '*iOffset
    if oCat.expanded:
        sButton = '-'
    else:
        if oCat.tree_children:
            sButton = '+'
        else:
            sButton = '.'
    print("{0}{1}{2}".format(sOffset,sButton,oCat.name))
    if oCat.expanded:
        for oChild in oCat.tree_children:
            print_tree_info(oChild,iOffset+1)  

it printed

-ROOT_NODE
   +major1
   .base2

which is great.

Now, passing the same node structure into the render function of a mako template (along with the mako template itself) I get the attribute error.

Here's how I render the template:

template = Template(..........)
html = template.render(category=root_node, item_template=template)

Here's the template

%if category is UNDEFINED:
ERROR
%elif category:
<div class="tree_item"  category_id="${category.id}">
%if category.expanded:
    <a class="collapse_tree_item"  category_id="${category.id}">-</a>
%elif category.tree_children:
    <a class="expand_tree_item" category_id="${oCategory.id}">+</a>
%endif
<a class="select_tree_item">${category.name}</a>
%if category.expanded:
    %for oChild in category.tree_children:
          ${item_template.render(category=oChild,item_template=item_template)}
    %endfor
%endif
</div>
%endif
Was it helpful?

Solution

<a class="expand_tree_item" category_id="${oCategory.id}">+</a>

should be

<a class="expand_tree_item" category_id="${category.id}">+</a>

Lesson learned: be consistent in your naming conventions.

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