Olivier Pons
Olivier Pons

Reputation: 15796

Conditional variable assignment

I'm doing this in one of my templates:

{% if activite.travel %}
    {% with activite.travel.personne as personne %}

        {% include 'includes/person_detail.html' %}

    {% endwith %}
{% endif %}
{% if activite.relation %}
    {% with activite.relation.src as personne %}

        {% include 'includes/person_detail.html' %}

    {% endwith %}
{% endif %}

Note: I may have more fields to come in activite that's why I'm not doing "else" but two separate "if".

I'd like to do something like:

{% if activite.travel %}
    {% set personne=activite.travel.personne %}
{% elsif activite.relation %}
    {% set personne=activite.relation.src %}
{% endif %}
{% include 'includes/person_detail.html' %}

Is there a way to do this in the template?

Upvotes: 0

Views: 959

Answers (1)

Sayse
Sayse

Reputation: 43320

Not exactly... but you can use with inside of include

{% if activite.travel %}
    {% include 'includes/person_detail.html' with personne=activite.travel.personne %}
{% elif activite.relation %}
    {% include 'includes/person_detail.html' with personne=activite.relation.src %}
{% endif %}

Upvotes: 1

Related Questions