Summary of the comments as answer and examples on both mentioned f:for
and f:groupedFor
view-helpers. f:for
is basically good to any kind of array and allows to iterate over each level of nested data - f:group
is good to transfor flat data structures into nested data-sets.
f:for
The input array is two-dimensional and looks like the following
$this->view->assign('nestedItems', [
'2016' => [
['title' => '1st Title', 'content' => '1st Content'],
['title' => '2nd Title', 'content' => '2nd Content'],
],
'2015' => [
['title' => '3rd Title', 'content' => '3rd Content'],
['title' => '4th Title', 'content' => '4th Content'],
],
]);
The Fluid template to iterate over that nested data-set looks like this
<f:for each="{nestedItems}" as="items" key="currentYear">
<h2>{currentYear}</h2>
<f:for each="{items}" as="item">
<h3>{item.title}</h3>
<p>{item.content}</p>
</f:for>
</f:for>
f:groupedFor
The flat input array looks like this (year is now part of each array element)
$this->view->assign('flatItems', [
['year' => 2016, 'title' => '1st Title', 'content' => '1st Content'],
['year' => 2016, 'title' => '2nd Title', 'content' => '2nd Content'],
['year' => 2015, 'title' => '3rd Title', 'content' => '3rd Content'],
['year' => 2015, 'title' => '4th Title', 'content' => '4th Content'],
]);
The Fluid template to iterate over that nested data-set looks like this
<f:groupedFor each="{flatItems}" as="items" groupBy="year" groupKey="currentYear">
<h2>{currentYear}</h2>
<f:for each="{items}" as="item">
<h3>{item.title}</h3>
<p>{item.content}</p>
</f:for>
</f:groupedFor>
Output for both scenarios
The output is the same in both scenarios
<h2>2016</h2>
<h3>1st Title</h3>
<p>1st Content</p>
<h3>2nd Title</h3>
<p>2nd Content</p>
<h2>2015</h2>
<h3>3rd Title</h3>
<p>3rd Content</p>
<h3>4th Title</h3>
<p>4th Content</p>
<f:debug>{myArray}</f:debug>
? - Jost