Arranging items left and right within a foreach loop

If you need to arrange items left and while performing a {foreach...} loop, you can determine the module of the index property of each item during the iteration.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{foreach $pages.data as $page}
    <div class="{if $page@index%2 == 1}right{else}left{/if}">
        {$page.post_title}
    </div>
{/foreach}
 
{*
index 0%2 = 0
index 1%2 = 1
index 2%2 = 0
index 3%2 = 1
index 4%2 = 0
index 5%2 = 1
*}
{foreach $pages.data as $page}
    <div class="{if $page@index%2 == 1}right{else}left{/if}">
        {$page.post_title}
    </div>
{/foreach}

{*
index 0%2 = 0
index 1%2 = 1
index 2%2 = 0
index 3%2 = 1
index 4%2 = 0
index 5%2 = 1
*}