I use Jinja2 “recursive” tag in my code,but how can I get the depth of current loop?

Try an array of loop. Index values where the parent index is saved before each call to loop() and popped immediately after. To modify an array without rendering text, enable {% do ... %} statements.

Try an array of loop. Index values where the parent index is saved before each call to loop() and popped immediately after. To modify an array without rendering text, enable {% do ... %} statements.

Example: template = """ {%- set idxs = 0 -%} {%- for item in sitemap recursive %} depth={{idxs|length}}. Idx={{loop. Index}}.

Pidx={{idxs-1}}. Title={{item. Title}} {%- if item.

Children -%} {%- do idxs. Append(loop. Index) -%} {{ loop(item.

Children) }} {%- do idxs.pop() -%} {%- endif %} {%- endfor %} """ class Node(): def __init__(self, title, children=): self. Title = title self. Children = children sitemap = Node('a', Node('a_a', Node('a_a_a'), ), Node('a_b', Node('a_b_a', Node('a_b_a_0'), ), ), ), Node('b'), env = jinja2.

Environment(extensions='jinja2.ext. Do') print env. From_string(template).

Render(sitemap=sitemap) This program prints: depth=1. Idx=1. Pidx=0.

Title=a depth=2. Idx=1. Pidx=1.

Title=a_a depth=3. Idx=1. Pidx=1.

Title=a_a_a depth=2. Idx=2. Pidx=1.

Title=a_b depth=3. Idx=1. Pidx=2.

Title=a_b_a depth=4. Idx=1. Pidx=1.

Title=a_b_a_0 depth=1. Idx=2. Pidx=0.

Title=b.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions