Question

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?

Was it helpful?

Solution

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

This code should work

 Total: {{ t|sum(attribute='size') }}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top