templates - 在 jinja 的循环中求和一个值

标签 templates variables 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>

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

最佳答案

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

这段代码应该可以工作

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

关于templates - 在 jinja 的循环中求和一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23657796/

相关文章:

javascript - 增加和减少一个变量,直到在 Javascript 中达到一个数字

java - 为什么我不能通过按 'enter' 来中断 while 循环

c++ - std::array 的读取范围

python - 在 Python 3 中使用多个输入变量

c - 如何限制 C 中另一个进程或函数使用(写/读)文件的访问?

shell - shell中使用sed声明变量

Python,如何在多个无限循环之间切换?

c++ - "incomplete type"关于使用 C++ 单位 units::compose

c++ - 为什么没有 std::make_raw_storage_iterator?

c++ - 如何在 C++ 编译时打印一个完整的模板参数