Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If statement in twig

Tags:

html

twig

symfony

Is possible to make an if statement in twig syntax and depending of the result close the html tag in a way or another. For example:

<button name="button" class="btn btn-warning"
    {% if someCondition == false %}

        > {#if is false close the button tag#}

        <i class="fa fa-gift"></i><strong> Welcome<br />Present</strong>
    {% else %}

        disabled="disabled">{#if is true disable the button tag and then close#}

        <i class="fa fa-gift"></i> Welcome<br />Present
    {% endif %}
</button>

Thanks in advance.

-UPDATE-

Sorry I made a silly mistake, I was doing the right thing but in the wrong place that's why it did not reflected on my site. Thanks anyway (:

like image 899
OmarAguinaga Avatar asked Sep 18 '25 02:09

OmarAguinaga


1 Answers

Your code looks right.

My way to do it :

<button name="button" class="btn btn-warning" {% if someCondition == true %} disabled="disabled" {% endif %}>

    <i class="fa fa-gift"></i><strong> Welcome<br />Present</strong>

</button>

If you need to add the strong tag, you can add a class :

<button name="button" class="btn btn-warning" {% if someCondition == true %} disabled="disabled" {% endif %}>

    <i class="fa fa-gift {% if someCondition == true %} strongClass {% endif %}"></i><strong> Welcome<br />Present</strong>

</button>

You can just do :

{% if someCondition == true %}
    <button name="button" class="btn btn-warning" disabled="disabled">
        <i class="fa fa-gift"></i><strong> Welcome<br />Present</strong>
    </button>
{% else %}
    <button name="button" class="btn btn-warning">
        <i class="fa fa-gift"></i>Welcome<br />Present
    </button>
{% endif %}
like image 68
Nicolas Avatar answered Sep 20 '25 17:09

Nicolas