the situation
the problem
I have found a way to handle the requirement. But with the following way, I need to duplicate some code which seems very wrong to me.
Current Code:
{% if activateShuffle == 'yes' %}
{% for item in images|shuffle() %}
<div>
<h1>{{ item.title }}</h1>
<img src="{{ asset('images/')}}{{ item.imagename }}"/>
</div>
{% endfor %}
{% else %}
{% for item in images %}
<div>
<h1>{{ item.title }}</h1>
<img src="{{ asset('images/')}}{{ item.imagename }}"/>
</div>
{% endfor %}
{% endif %}
the solution
is there a way to define the if condition within the for loop? so that it checks if a variable has a specific value and adds the extension? So maybe something like this:
{% for item in images if activateShuffle == 'yes' add |shuffle() %}
<div>
<h1>{{ item.title }}</h1>
<img src="{{ asset('images/')}}{{ item.imagename }}"/>
</div>
{% endfor %}
Thank you for your help
You could change the images
variable depening on the value of activateShuffle
before your loop like this:
{% if activateShuffle == 'yes' %}
{% set images = images|shuffle() %}
{% endif %}
{% for item in images %}
<div>
<h1>{{ item.title }}</h1>
<img src="{{ asset('images/')}}{{ item.imagename }}"/>
</div>
{% endfor %}