Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig - interpolating variables

I have the following:

{% if promo.monday_unavailable == 1 %} 
    not available mondays 
{% elseif promo.monday_available == 1%}
    available mondays 
{% else %}
    available mondays from {{promo.monday_start}} until {{promo.monday_end}}
{% endif %}
<br />
{% if promo.tuesday_unavailable == 1 %} 
    not available tuesdays 
{% elseif promo.tuesday_available == 1%}
    available tuesdays 
{% else %}
    available tuesdays from {{promo.tuesday_start}} until {{promo.tuesday_end}}
{% endif %}
<br />

...

That I would like to do for each day of the week.

I'm wondering if there is a way I can simplify the code to read

{% for i in ['monday','tuesday','wednesday','thursday','friday','saturday','sunday'] %}

{% if promo.~i~"_unavailable" == 1 %} 
    not available mondays 
{% elseif promo.~i~"_available" == 1%}
    available mondays 
{% else %}
    available mondays from {{promo.~i~"_start"}} until {{promo.~i~"_end"}}
{% endif %}
<br />

{% endfor %}

With Twig.

Any help would be appreciated. I'm at a loss for what keywords to search for anymore.

like image 907
user1439590 Avatar asked Dec 03 '22 19:12

user1439590


2 Answers

I know this is an old thread but twig has support for inline interpolation like:

{{i18n("language_#{langId}")}}

Important that the string to interpolated is with double-quotes.

like image 57
Niclas Avatar answered Dec 05 '22 09:12

Niclas


Found answer by mashing my forehead on the keyboard.

rather than

{% if promo.~i~"_unavailable" == 1 %} 

use

{% promo[i~"_unavailable"] == 1 %)
like image 41
user1439590 Avatar answered Dec 05 '22 08:12

user1439590