Skip to content

Instantly share code, notes, and snippets.

@vanduc1102
Last active June 17, 2019 04:51
Show Gist options
  • Save vanduc1102/9da5150759b6835e07ef16e78bc79794 to your computer and use it in GitHub Desktop.
Save vanduc1102/9da5150759b6835e07ef16e78bc79794 to your computer and use it in GitHub Desktop.
TWIG - Swap element in a loop , swapping 444 and 222
{# to run the code https://twigfiddle.com/ #}
{% set arr = [1, 2 , 3 , 444, 222, 5 ,6 ,7 ,8 ,9]%}
{% set temp = null %}
{% for item in arr %}
{% if item == 444 %}
{% set temp = item %}
{% set item = arr[loop.index0 +1 ] %}
{% endif %}
{% if temp != item and temp is not null and loop.index < loop.length %}
{% set item = arr[loop.index0 +1 ] %}
{% endif %}
{% if loop.index0 == 4 %}
{% set item = temp %}
{% set temp = null %}
{% endif %}
<li>{{item}}</li>
{% endfor%}
@vanduc1102
Copy link
Author


    2;

    3;

    222;

        444;

    5;

    6;

    7;

    8;

    9;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment