Silverstripe Version: 4 & 5
Question:
Hi I’m a SS newbie.
How can I render complex data struture like a nested ArrayList/ArrayData (dict/list) to the templates.
I have a poor understanding on https://docs.silverstripe.org/en/5/developer_guides/templates/rendering_templates/
To keep it simple, I found the following code works:
public function getTestProducts()
{
$products = [
[
'Name' => 'Product 1',
'ID' => 1,
],
[
'Name' => 'Product 2',
'ID' => 2,
],
];
$data = ArrayList::create($products);
return $data;
}
while the code belows does not work. I can’t get Stations.
public function getTestProducts()
{
$products = [
[
'Name' => 'Product 1',
'ID' => 1,
'Stations' => [
[
'Location' => 'Station A',
'Color' => 'Red',
],
[
'Location' => 'Station B',
'Color' => 'Blue',
],
],
],
[
'Name' => 'Product 2',
'ID' => 2,
],
];
$data = ArrayList::create($products);
return $data;
}
I want to use it in template like this
<% loop $TestProducts %>
<h2>$Name</h2>
<h2>$ID</h2>
<% loop $Stations %>
<h3>$Location</h3>
<% end_loop %>
<% end_loop %>
Besides, can anyone give some examples how to render the data? I can’t understand when I should return a object like code above, when should I use return $this->customise($data), or $this->customise($data)->renderWith().