Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-20T14:36:12.407Z
Answers.LastActivityDate: 2016-08-20T14:41:58.930Z
Answers.LastEditDate: 2016-08-20T14:41:58.930Z
Answers.UserDisplayName: AbbeyDesign
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-02-21T13:18:46.143Z
Answers.LastActivityDate: 2022-02-21T13:18:46.143Z
Answers.UserDisplayName: MoritzLost
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-20T20:38:09.187Z
Answers.LastActivityDate: 2016-08-20T20:38:09.187Z
Answers.UserDisplayName: Marion Newlevant
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-08-20T14:21:43.177Z
Comments.Text: Where does the "1" come from? Did you mean "3 4"?
Comments.UserDisplayName: AbbeyDesign
Tags: <templating><forloop><group><math>
Title: Total sum within loop
ThreadText: # 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](http://twig.sensiolabs.org/doc/tags/set.html) tag to do assignments, and the [+](http://twig.sensiolabs.org/doc/templates.html#math) operator to do addition. When doing math inside a [for](http://twig.sensiolabs.org/doc/tags/for.html) 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](http://twig.sensiolabs.org/doc/templates.html)
  
  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](https://twig.symfony.com/doc/2.x/filters/filter.html), [map](https://twig.symfony.com/doc/2.x/filters/map.html) and [reduce](https://twig.symfony.com/doc/2.x/filters/reduce.html). 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:
  
  ```twig
  {% 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 }} <br>
  totalSeatsParents: {{ totalSeatsParents }} <br>
  totalSeatsAll: {{ totalSeatsAll }} <br>
  
  ```
  
  
  ---
  
  