title: Twig: Update variable located earlier in code
body: <p>I need to show a value at the top of a page, which needs to be updated after I have run some Twig loops in the middle of the page.</p>
  
  <p>Here's an example:</p>
  
  <pre><code>&lt;div&gt;Total Amount: {{ totalAmount }}&lt;/div&gt;
  {% for product in products %}
      {% set totalAmount = totalAmount + product.amount %}
  {% endfor %}
  </code></pre>
  
  <p>I could compute the value in the Symfony controller, but there are certain reasons why I would prefer doing this in the Twig template.</p>
  
  <p>Is the above possible with Twig? I'm sure the Twig code is executed sequentially and I may have to find a different solution. But I'd like to see if anyone has any suggestions.</p>
  
  <p>Thanks,</p>
  
  <p>JB</p>