<p>You could use some twig math, found at the bottom of the page here:</p>

<p><a href="http://twig.sensiolabs.org/doc/templates.html" rel="nofollow">Twig Templating</a></p>

<p>Not sure without seeing the rest of your code, but you could do something like this:</p>

<pre><code>{% set totals = [3,4]%}
{% for total in totals %}
  {{ total }}
  {% set sum = total + sum ?? 0 %}
  {% if loop.last %}{{ sum }}{% endif %}
{% endfor %}
</code></pre>

<p><strong>Should output 34 and lastly 7</strong></p>

<p>Or more specifically to your code, roughly (not tested)</p>

<pre><code>{% for total in thing %}
  {{ total.seatsKids }} {{ total.seatsParents }}
  {% set sum = total.seatKids + total.seatsParents + sum ?? 0 %}
  {% if loop.last %}{{ sum }}{% endif %}
{% endfor %}
</code></pre>

<p><strong>Should output 123456 and lastly 21</strong></p>

CC BY-SA 3.0
2016-08-20T14:36:12.407Z
2016-08-20T14:41:58.930Z
2016-08-20T14:41:58.930Z
AbbeyDesign
<p>Just to provide a simpler solution, current versions of Twig provide some filters borrowed from the functional programming paradigm to make operations like this easier: <a href="https://twig.symfony.com/doc/2.x/filters/filter.html" rel="nofollow noreferrer">filter</a>, <a href="https://twig.symfony.com/doc/2.x/filters/map.html" rel="nofollow noreferrer">map</a> and <a href="https://twig.symfony.com/doc/2.x/filters/reduce.html" rel="nofollow noreferrer">reduce</a>. With those methods, operations like this can be done in one line, reducing noise in your code and eliminating some common sources for bugs (like off-by-one errors).</p>
<p>Some examples for this specific requirement:</p>
<pre class="lang-twig prettyprint-override"><code>{% set seatCounts = [
    { seatsKids: 5, seatsParents: 7, },
    { seatsKids: 6, seatsParents: 3, },
    { seatsKids: 0, seatsParents: 4, },
] %}

{% set totalSeatsKids = seatCounts|reduce((carry, current) =&gt; carry + current.seatsKids, 0) %}
{% set totalSeatsParents = seatCounts|reduce((carry, current) =&gt; carry + current.seatsParents, 0) %}

{% set totalSeatsAll = seatCounts
    |map(current =&gt; current.seatsKids + current.seatsParents)
    |reduce((carry, current) =&gt; carry + current)
%}

totalSeatsKids: {{ totalSeatsKids }} &lt;br&gt;
totalSeatsParents: {{ totalSeatsParents }} &lt;br&gt;
totalSeatsAll: {{ totalSeatsAll }} &lt;br&gt;
</code></pre>

CC BY-SA 4.0
2022-02-21T13:18:46.143Z
2022-02-21T13:18:46.143Z
MoritzLost
<p>To do math in twig, you will need to use the <a href="http://twig.sensiolabs.org/doc/tags/set.html">set</a> tag to do assignments, and the <a href="http://twig.sensiolabs.org/doc/templates.html#math">+</a> operator to do addition. When doing math inside a <a href="http://twig.sensiolabs.org/doc/tags/for.html">for</a> loop, you will need to keep in mind that</p>

<blockquote>
  <p>loops are scoped in Twig; therefore a variable declared inside a for loop is not accessible outside the loop itself.
  If you want to access the variable, just declare it before the loop.</p>
</blockquote>

<p>It looks as if you have a collection of things that represent seat counts, and you want to loop over them and calculate the totals. So, something like this:</p>

<pre><code>{# define and intialize our variables outside the loop #}
{% set kidSeatTotal = 0 %}
{% set parentSeatTotal = 0 %}

{# loop over the seatCounts and add them up #}
{% for seatCount in thing %}
  {% set kidSeatTotal = kidSeatTotal + seatCount.seatsKids %}
  {% set parentSeatTotal = parentSeatTotal + seatCount.seatsParents %}
{% endfor %}

{# maybe we want the grand total as well #}
{% set grandSeatTotal = kidSeatTotal + parentSeatTotal %}

{# now we can output them: #}
kid seat total: {{ kidSeatTotal }}
parent seat total: {{ parentSeatTotal }}
grand total: {{ grandSeatTotal }}
</code></pre>

CC BY-SA 3.0
2016-08-20T20:38:09.187Z
2016-08-20T20:38:09.187Z
Marion Newlevant
<p>I have simple loop:</p>
<pre><code>{% for total in thing %}
  {{ total.seatsKids }} {{ total.seatsParents }}
{% endfor %}
</code></pre>
<p>This results in a string of numbers eg. <code>1 3 4</code>. I want the sum of all numbers. So how do I get <code>8</code> at the end? I don't understand how to write math operations within loops.</p>

CC BY-SA 3.0
2016-08-20T14:21:43.177Z
Where does the "1" come from? Did you mean "3 4"?
AbbeyDesign
<templating><forloop><group><math>
Total sum within loop
# Total sum within loop

(Asked by: username_0 on 2016-08-18)

I have simple loop:
{% for total in thing %}
  {{ total.seatsKids }} {{ total.seatsParents }}
{% endfor %}

This results in a string of numbers eg. 1 3 4. I want the sum of all numbers. So how do I get 8 at the end? I don't understand how to write math operations within loops.

**Tags:** templating, forloop, group, math

> **Comments:**
> **username_1**: Where does the "1" come from? Did you mean "3 4"?  

---

## 3 answers

### Answer by username_3 on 2016-08-20. Score: 13 | Accepted answer

To do math in twig, you will need to use the set tag to do assignments, and the + operator to do addition. When doing math inside a for loop, you will need to keep in mind that

loops are scoped in Twig; therefore a variable declared inside a for loop is not accessible outside the loop itself.
  If you want to access the variable, just declare it before the loop.

It looks as if you have a collection of things that represent seat counts, and you want to loop over them and calculate the totals. So, something like this:
{# define and intialize our variables outside the loop #}
{% set kidSeatTotal = 0 %}
{% set parentSeatTotal = 0 %}

{# loop over the seatCounts and add them up #}
{% for seatCount in thing %}
  {% set kidSeatTotal = kidSeatTotal + seatCount.seatsKids %}
  {% set parentSeatTotal = parentSeatTotal + seatCount.seatsParents %}
{% endfor %}

{# maybe we want the grand total as well #}
{% set grandSeatTotal = kidSeatTotal + parentSeatTotal %}

{# now we can output them: #}
kid seat total: {{ kidSeatTotal }}
parent seat total: {{ parentSeatTotal }}
grand total: {{ grandSeatTotal }}


---

### Answer by username_1 on 2016-08-20. Score: 2

You could use some twig math, found at the bottom of the page here:
Twig Templating
Not sure without seeing the rest of your code, but you could do something like this:
{% set totals = [3,4]%}
{% for total in totals %}
  {{ total }}
  {% set sum = total + sum ?? 0 %}
  {% if loop.last %}{{ sum }}{% endif %}
{% endfor %}

Should output 34 and lastly 7
Or more specifically to your code, roughly (not tested)
{% for total in thing %}
  {{ total.seatsKids }} {{ total.seatsParents }}
  {% set sum = total.seatKids + total.seatsParents + sum ?? 0 %}
  {% if loop.last %}{{ sum }}{% endif %}
{% endfor %}

Should output 123456 and lastly 21


---

### Answer by username_2 on 2022-02-21. Score: 1

Just to provide a simpler solution, current versions of Twig provide some filters borrowed from the functional programming paradigm to make operations like this easier: filter, map and reduce. With those methods, operations like this can be done in one line, reducing noise in your code and eliminating some common sources for bugs (like off-by-one errors).
Some examples for this specific requirement:
{% set seatCounts = [
    { seatsKids: 5, seatsParents: 7, },
    { seatsKids: 6, seatsParents: 3, },
    { seatsKids: 0, seatsParents: 4, },
] %}

{% set totalSeatsKids = seatCounts|reduce((carry, current) => carry + current.seatsKids, 0) %}
{% set totalSeatsParents = seatCounts|reduce((carry, current) => carry + current.seatsParents, 0) %}

{% set totalSeatsAll = seatCounts
    |map(current => current.seatsKids + current.seatsParents)
    |reduce((carry, current) => carry + current)
%}

totalSeatsKids: {{ totalSeatsKids }} 
totalSeatsParents: {{ totalSeatsParents }} 
totalSeatsAll: {{ totalSeatsAll }}


---

