Twig sum row above the summerisation
You can extend twig by adding extra filters http://symfony.com/doc/current/cookbook/templating/twig_extension.html
In your case, you can use array_sum function:
public function getFilters()
{
return array(
new \Twig_SimpleFilter('sum', 'array_sum'),
);
}
if you don't want to use the controller and want to do the summing in twig then try using the set command:
{# do loop first and assign whatever output you want to a variable #}
{% set sum = 0 %}
{% set loopOutput %}
{% for r in a.numbers}
{% set sum = sum + r.number %}
{% endfor %}
{% endset %}
sum is: {{ sum }}
{# some content.. #}
{{ loopOutput }}
I assume the loop was in a specific place because it is intended to output something into the template, this allows you to rearrange the load order while still displaying as you want.