In Silverstripe 4, I'd like to use two Loops on a single page template. The arrays are created inside of a single function inside my Page Controller.
My idea was to create two ArrayLists, then combine them into a third ArrayList, which I pass to the template.
Using SQLSelect, I have some code that creates an ArrayList of data. $queryArray is an array of key=>value pairs.
$sqlQuery = new SQLSelect();
$sqlQuery->setFrom('Wine');
$sqlQuery->addWhere($queryArray);
$results = $sqlQuery->execute();
$SSArrayList = new ArrayList; //new ArrayList;
foreach($results as $result) {
$SSArrayList->push(new ArrayData($result));
}
I have some other code that creates another ArrayList from the same $results:
foreach($results as $result) {
if (!empty($result['BrandName'])) {
$JSBrandsArray->push(array('Brandname'=>$result['BrandName']));
}
}
Then, the third ArrayList combines these two arrays:
$mainArray = new ArrayList;
$mainArray->push($SSArrayList);
$mainArray->push($JSBrandsArray);
$mainArray is passed to the template like so:
return $this->customise(array('MainArray'=>$mainArray))->renderWith("Layout/WinesList");
Then, in the WinesList.ss template, I thought I could do something like this:
<% loop $MainArray %>
<% loop $SSArrayList %>
// show results from $SSArrayList
<% end_loop %>
<% loop $JSBrandsArray %>
// show results from $JSBrandsArray
<% end_loop %>
<% end_loop %>
If I var_dump() $mainArray from the page controller, $mainArray seems to have all the data, but I can't figure out how to properly access the data from the template.
Is this even possible? If so, what am I doing wrong?