Mako Template Recursion: AttributeError: l'oggetto "indefinito" non ha attributo 'ID'
-
12-12-2019 - |
Domanda
Ho fatto un modello per rappresentare una struttura ad albero.Ogni nodo dell'albero ha un ID, un nome, un elenco di bambini (Tree_children) e una proprietà espansa.
Ho organizzato alcuni nodi in una struttura ad albero e quindi ha chiamato la seguente funzione con il nodo root:
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)
.
stampato
-ROOT_NODE
+major1
.base2
.
che è fantastico.
Ora, passando la stessa struttura del nodo nella funzione di rendering di un modello di mako (insieme al modello Mako stesso) ottengo l'errore di attributo.
Ecco come renderò il modello:
template = Template(..........)
html = template.render(category=root_node, item_template=template)
.
Ecco il modello
%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
. Soluzione
<a class="expand_tree_item" category_id="${oCategory.id}">+</a>
.
dovrebbe essere
<a class="expand_tree_item" category_id="${category.id}">+</a>
.
Lezione appresa: essere coerente nelle tue convenzioni di denominazione.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow