How do I return the largest and smallest value inside a for loop using Twig?

0

I make a listing of some products and would need to return the largest and smallest value of the products without using max and min in the SQL query. I've tried using Twig's max () function but it returns some errors. Here is what I did and the error that is happening:

Code:

{% for product in produtos %}
        {% set maxValue = max(product.valor)%}
        Valor--->>{{maxValue}}
{% endfor %}

Error:

An exception has been thrown during the rendering of a template ("max() [<a href='function.max'>function.max</a>]: When only one parameter is given, it must be an array")
    
asked by anonymous 21.10.2015 / 14:16

1 answer

1

Try this way

{% set maxValue = 0 %} 
{% for product in produtos %} 
{% set maxValue = max(product.valor, maxValue) %} 
{% endfor %} 
{{ maxValue }}
    
21.10.2015 / 20:37