Last active
June 17, 2019 04:51
-
-
Save vanduc1102/9da5150759b6835e07ef16e78bc79794 to your computer and use it in GitHub Desktop.
TWIG - Swap element in a loop , swapping 444 and 222
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# 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%} |
Author
vanduc1102
commented
Jun 17, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment