Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum a value inside a loop in jinja

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?

like image 586
antonis_man Avatar asked May 14 '14 14:05

antonis_man


1 Answers

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

This code should work

 Total: {{ t|sum(attribute='size') }}
like image 101
Pirata21 Avatar answered Nov 15 '22 12:11

Pirata21