eZ Publish 4.x/5.x Legacy で子アイテムを2個単位でグルーピングしたい時のテンプレート処理。
テンプレートの記述例
<div class="level-1">
{foreach $node.children as $key => $child}
{if eq( $key|mod( 2 ), 0 )}
<div class="level-2">
{/if}
<div class="level-3">{$child.name|wash()}</div>
{if or( ne( $key|mod( 2 ), 0 ), eq( $key, $category.children_count|dec ) )}
</div>
{/if}
{/foreach}
</div>
出力 HTML
<div class="level-1">
<div class="level-2">
<div class="level-3">アイテム1</div>
<div class="level-3">アイテム2</div>
</div>
<div class="level-2">
<div class="level-3">アイテム3</div>
<div class="level-3">アイテム4</div>
</div>
<div class="level-2">
<div class="level-3">アイテム5</div>
</div>
</div>
解説
子アイテムを {foreach}
でループし、キーが偶数ならヘッダー処理、奇数か最後尾ならフッター処理、という流れになっている。
最後尾の判定は、子アイテムの数を $node
オブジェクトの children_count
属性から取得して、dec
オペレーターでデクリメントした値を使っている。