title: How to add values in Thymeleaf variable in the loop and after complete loop display final value
body: <p>I am design bill for products, I want to display all products in thymeleaf template using table and at last, out of loop I want to display sum of price of all products in thymeleaf. how can I define global variable and do it?</p>
  
  <pre><code>&lt;table th:with="totalPrice=0"&gt;
      &lt;tr th:each="count,iterator: ${product.paidService}"&gt;
          &lt;td th:text="${iterator.index+1}"&gt;&lt;/td&gt;
          &lt;td th:text="${count.name}"&gt;&lt;/td&gt;
          &lt;td&gt;Paid&lt;/td&gt;
          &lt;td th:text="${count.price}"&gt;&lt;/td&gt;
          &lt;p th:with="totalPrice=${totalPrice + count.price}"&gt; &lt;/p&gt;
      &lt;/tr&gt;
      &lt;span th:text="${totalPrice}"&gt;&lt;/span&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>I am geeting <code>0</code> as output but I want sum of all products price as output.</p>
  
  <p>How to make variable global and solve my problem?</p>