Sum value inside loop in jinja
I have a for loop in jinja template:
<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>
I want to sum the value t.sizeand use it as the total size, but above this block. What is the best way to do this?
+4
1 answer
You should use the jinja function SUM => http://jinja.pocoo.org/docs/
This code should work
Total: {{ t|sum(attribute='size') }}
+7