Error comparing two strings in an if in twig

2

I have an error comparing two strings in twig .

Here is an example of my code:

{% set var1 = "" %}
{% set var2 = "" %}
{% for unidad in array %}
    {% if unidad.var1 == var1 %}
        {% if unidad.var2 == var2 %}
            <tr>
                <td>{{ habitacion.var1 }}</td>
                <td>{{ habitacion.var2 }}</td>
            </tr>
        {% else %}
            <tr><td>---</td><td>---</td></tr>
        {% endif %}
    {% else %}
        <tr>
            <td>|{{ unidad.var1 }}| |{{ var1 }}|</td>
            <td>|{{ unidad.var2 }}| |{{ var2 }}|</td>
        </tr>
    {% endif %}
    {% set var1 = unidad.var1 %}
    {% set var2 = unidad.var2 %}
{% endfor %}

The problem is that the loop does not enter the if , with the two variables being the same in some cases. I show you the output screen in a loop loop that does not enter the if :

<td>|NORMAL| |NORMAL|</td>
<td>|311| |311|</td>

I have also tried to put the if in this way:

{% if unidad.var1 is same as(var1) %}

But it does not work either.

    
asked by ArtEze 02.08.2016 в 09:18
source

1 answer

0

I have a theory about it, can it be that in some of the replacements the value of unit.var1 and unit.var2 is false or null or something similar?

Remember that twig behaves very similar to PHP so if you compare a string with false means that there is no string so it would give false and it would not show you this row.

Can you add some sample data?

greetings

    
answered by 03.04.2018 в 15:33