Sum a value inside a loop in jinja

jinja2, loops, templates, variables

Solution

You must use the jinja function SUM => http://jinja.pocoo.org/docs/

This code should work

 Total: {{ t|sum(attribute='size') }}

Problem

I have a for loop in a 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> ``` what I want is to sum the value of `t.size` and use it as a total size number, but above this block. What is the best way to do that?

Original source