Mako 템플릿 재귀 : AttributeError : 'Undefined'객체에는 속성 'ID'가 없습니다.
-
12-12-2019 - |
문제
트리 구조를 나타내는 템플릿을 만들었습니다.트리의 각 노드에는 ID, 이름, 어린이 목록 (TREE_CHILDREN) 및 확장 된 속성이 있습니다.
트리 구조에 몇 개의 노드를 배열 한 다음 루트 노드로 다음 함수를 호출합니다.
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)
.
인쇄
-ROOT_NODE
+major1
.base2
.
그것은 훌륭합니다.
이제, Mako 템플리트와 함께 Mako 템플리트의 렌더링 기능에 동일한 노드 구조를 전달합니다 (Mako 템플리트 자체와 함께) 속성 오류가 발생합니다.
여기에 내가 템플릿을 렌더링하는 방법 :
template = Template(..........)
html = template.render(category=root_node, item_template=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
. 해결책
<a class="expand_tree_item" category_id="${oCategory.id}">+</a>
.
는
이어야합니다<a class="expand_tree_item" category_id="${category.id}">+</a>
.
수업 배운 : 귀하의 명명 규칙에서 일관성을 유지하십시오.
제휴하지 않습니다 StackOverflow