在jinja中循环一个值

ant*_*man 4 variables templates loops jinja2

我在jinja模板中有一个for循环:

          <ul>
                {%- for t in tree recursive %}
                    <li><span class="li_wrap">
                        <span><i class="glyphicon {{ 'glyphicon-folder-close' if t.type == 'folder' else 'glyphicon-file'}}"></i> {{ t.name }}</span>
                        {% if t.type != 'folder' %}
                            <span class="pull-right">Size: {{ t.size/1000 }} kb </span> 
                        {% endif %}
                        </span>
                    {%- if t.children -%}
                        <ul>{{ loop(t.children) }}</ul>
                    {%- endif %}
                    </li>
                {%- endfor %}
          </ul>
Run Code Online (Sandbox Code Playgroud)

我想要的是将其值相加t.size并将其用作总大小数,但高于此块.最好的方法是什么?

小智 7

你必须使用jinja函数SUM => http://jinja.pocoo.org/docs/

这段代码应该有效

 Total: {{ t|sum(attribute='size') }}
Run Code Online (Sandbox Code Playgroud)