Check for number less than x in array
Twig isn't very well equipped to deal with more than basic logic and comparisons, but you can accomplish some of these tests with a bit of extra juggling:

{% set constraintMet = false %}

{% for value in [0, 30, 50, 10, 9, 200] %}
  {# Here’s where you get to set up your advanced comparison criteria: #}
  {% if not constraintMet and value &lt; 10 %}
    {% set constraintMet = true %}
  {% endif %}
{% endfor %}

Constraint {{ constraintMet ? "was" : "wasn't" }} met.


Alternatively, you can build the conditional into your for loop…

{% set constraintMet = false %}

{% for value in [0, 30, 50, 10, 9, 200] if not constraintMet and value &lt; 10 %}
  {% set constraintMet = true %}
{% endfor %}

Constraint {{ constraintMet ? "was" : "wasn't" }} met.


…but this is a little more difficult to read, in my opinion, as it obscures the critical test. One improvement this offers, though, is that it prevents the loop from running after the constraint is met, unlike the first option, which runs/renders the body of the if block for every element in the array—even if it finds a match early on.

Whenever possible, I'd recommend stashing this kind of logic in a custom Twig extension. If you haven't built a plugin or module before, Pluginfactory can set you down the right path!


  Heads up! map, filter, and reduce functions have come to Twig (and Craft, as of 3.2), which make this possible to collapse this into a one-line statement: {% set hasMatchingValue = [0, 30, 50, 10, 9, 200] | filter(v =&gt; v &lt; 10) | length &gt; 0 %}. This just iterates over the array, and filters out anything that is 10 or greater with the implicitly returned value from the “arrow function,” then checks if the new array includes “more than zero” values.