13

Are there any nice ways to use while and repeat loops in Twig? It is such a simple task, but without macros I can't find anything nice and simple.

At least do an infinite cycle and then break it in a condition?

EDIT:

I mean something like

do {
    // loop code
} while (condition)

or

while (condition) {
    // loop code
}

Edit 2:

Looks like it is not supported natively by twig same reason as it is not supported neither continue; or break; statements.

https://github.com/twigphp/Twig/issues/654

Aistis
  • 3,695
  • 2
  • 34
  • 34
  • There's a `for` and `foreach` statement in Twig, also `if` conditions, but what is your question exactly? – KhorneHoly Dec 29 '14 at 15:36
  • Added some examples what I am looking exactly. – Aistis Dec 29 '14 at 15:46
  • #Aistis I face the same issue... would need to use while(condition){ }... but seems that it's not available in TWIG. But you say that macro might be a way to do it. could you share more about it as I have never used macro with symfony so far. – Alexis_D Mar 13 '15 at 15:51

5 Answers5

22

You can emulate it with for ... in ... if by using a sufficiently-high loop limit (10000?)

while

PHP:

$precondition = true;
while ($precondition) {
    $precondition = false;
}

Twig:

{% set precondition = true %}
{% for i in 0..10000 if precondition %}
    {% set precondition = false %}
{% endfor %}

do while

PHP:

do {
    $condition = false;
} while ($condition) 

Twig:

{% set condition = true %} {# you still need this to enter the loop#}
{% for i in 0..10000 if condition %}
    {% set condition = false %}
{% endfor %}
fregante
  • 29,050
  • 14
  • 119
  • 159
12

I was able to implement a simple for loop in twig. So the following php statement:

for ($x = 0; $x <= 10; $x++) {
    echo "The number is: $x <br>";
}

when translated to twig is:

{% for i in 0..10 %}
    * {{ i }}
{% endfor %}

It's not a while loop but a potential workaround. The best suggestion is to leave business logic like this out of the template layer.

DslDip
  • 149
  • 2
  • 6
9

In a nutshell: no. This functionality implies advanced logic, which should be in your business logic, not in the template layer. It's a prime example of the separation of concerns in MVC.

Twig supports for-loops completely, which should suffice if you code correctly - being that complex conditional decisions on which data to display are taken in the business logic where they belong, which then pass a resulting array 'ready to render' to the templates. Twig then supports all nice features only needed for rendering.

Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136
  • 7
    The problem is that I need to print a nested array. There is a condition it has to be printed not recursively, but **iteratively**. I am not sure is it really so advanced logic that it should preprocessed in back-end. – Aistis Dec 29 '14 at 22:54
  • You need neither `do` nor `while` to process a nested array in Twig. What you are looking to do is probably easily done with a simple macro. Makes this question a glaring [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) though - you asked about your intended solution, not the real problem you're trying to solve. Now you leave me and the rest of us just guessing at what you *really* want to achieve. – Niels Keurentjes Dec 29 '14 at 23:01
  • 1
    Thank you Niels for your concern, but **I was interesting does Twig really do not have the implementation** of `do` and `while`. With the comment above, I've just wanted to point out, that there are some cases where these types of cycles could be a clever choice. I am not looking the answer for my particular problem. Sorry if we have misunderstood t each other. – Aistis Dec 29 '14 at 23:14
  • 2
    Well, that part was answered in the first four words of my answer ;) Twig does not support features its designers consider to be 'out of scope' for a template engine, which includes these advanced looping constructs. It only supports `for` for looping - although that's very powerful when combined with range constructs and the like. – Niels Keurentjes Dec 29 '14 at 23:14
  • 1
    Some extra thoughts on loops with breaking. https://github.com/twigphp/Twig/issues/654 – Aistis Apr 03 '16 at 21:07
  • 1
    I'm completely disagree. A do..while/while() loop isn't 'complex' it's a way to do something at least once and in DISPLAY LOGIC which doesn't mean I absolutely HAVE to make a giant pile of code to structure an array just to make my display work with the limited ability of for/foreach. – tlorens Jun 25 '19 at 12:53
2

This is possible, but a little bit complicated.

You can use {% include ... %} to process nested arrays, which from the comments I read is what you need to do.

Consider the following code:

nested_array_display.html

<ul>
{% for key, val in arr %}
    <li>
        {{ key }}:
{% if val is iterable %}
{% include 'nested_array_display.html' %}
{% else %}
        {{ val }}
{% endif %}
    </li>
{% endfor %}
</ul>
Xesau
  • 161
  • 1
  • 9
-1

Warning with the top solution with "high loop limit" : the loop doesn't break when the condition returns false, it just doesn't enter the loop. So the loop runs up to the high indice

Nico las
  • 1
  • 1