How to check if number is prime? How do I check if a given number is prime in twig?
Example using build-in functions (sure, not the best ;-) ).

Supposing that number is our number:

{% set result = 0 %}
{% if number / number|round == 1 %}
    {% set result = 1 %}
    {% if number &gt; 3 %}
        {% for i in 2..number-1 if result == 1 %}
            {% if number is divisibleby(i) %}
                {% set result = 2 %}
            {% endif %}
        {% endfor %}
    {% endif %}
{% endif %}


Now, result equals to:


0, if number is not integer
1, if number is prime 
2, if number is composite