Traxstar Traxstar - 4 months ago 18
Twig Question

Generating Table Content with TWIG in for loop

I've got a table, that content got generated in a twig template. Every column that got generated executes the code below.

What I need to do is the following: I need to check if the index of my array is equal to the value 'page.id'.

If it is equal it should check if the value of the index is > or <= 0. Then he should put following in the code:

<span class="uk-icon-check"><span>


If it is not equal it should set

<span class="uk-icon-close"><span>


The problem is, that there is no continue in TWIG, so I try to build myself one but it is still is not working.

The goal that in every col is the close or the ok sign.

At the moment there are some cols where is nothing in and that needs to be fixed.

Here is the full TWIG CODE :

<td class="{{class}}"><small>
{% set continue = false %}
{% for key,cache in moto %}
{% if key != page.id %}
<span class="uk-icon-close"><span>
{% set continue = true %}
{% endif %}
{% if not continue %}
{% if key == page.id %}
{% if cache > 0 %}
<span class="uk-icon-check"><span>
{% elseif cache <= 0 %}
<span class="uk-icon-close"><span>
{% endif %}
{% endif %}
{% endif %}
{% if continue %}
{% set continue = false %}
{% endif %}
{% endfor %}


Not sure where the mistake is... important is that there are not 2 signs in one row

Answer

Finaly i got it :

{% set break = 0%}
        {% for key,cache in moto  if break == 0%}
          {% if key == page.id and cache > 0 %}
            <span class="uk-icon-check"> {{cache}}<span>
            {% set break = 1%}
            {% else  %}
            <span class="uk-icon-exclamation"><span>
            {% set break = 1%}
          {% endif %}
        {% endfor %}