Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

[SOLVED] SS3 display content of array of DataObjects


Go to End


2 Posts   1194 Views

Avatar
Stephan

Community Member, 55 Posts

28 January 2014 at 5:34am

Hi experts,
I'm desperately trying to get a result of a data query to my template.

I have a product object and an accessories table.
I want to show all products which are an accessory of a given product.
Here is my code:

$Accessories = ProductAccessory::get()->filter(array("ProductSKU" => $strSKU));
$aryReturn = array();
foreach($Accessories as $AccessoryItem) {
	$aryReturn[] = Product::get()->filter(array("SKU" => $AccessoryItem->AccessorySKU))->First();
}

How can I get this array into a template? (the array is populated correctly, so thats not my problem)
I did try:
$data = array("Accessories" => $aryReturn);
$template = new SSViewer('ProductAccessoriesTable');
return $template->process(new ArrayData($data));

with ProductAccessoriesTable.ss

<% loop $Accessories %>
  <tr>
    <td>$Pos</td>
    <td>$SKU</td>
    <td>$ProductName</td>
  </tr>
<% end_loop %>

But this doesn't work :-(

What can I do?

TIA Stephan

Avatar
Stephan

Community Member, 55 Posts

28 January 2014 at 6:31am

Ok, my team-mate gave me the right solution.
Thank you, Jonas :-)

Here is the working code:

$Accessories = ProductAccessory::get()->filter(array("ProductSKU" => $strSKU));
$objReturn = new ArrayList();
foreach($Accessories as $AccessoryItem) {
	$objReturn->push(Product::get()->filter(array("SKU" => $AccessoryItem->AccessorySKU))->First());
}
$arydata = new ArrayData(array("Accessories" => $objReturn));
$template = new SSViewer('ProductAccessoriesTable');
return $template->process($arydata);

with

<table>
  <th>
    <td>SKU</td>
    <td>Produktname</td>
  </th>
<% loop $Accessories %>
  <tr>
    <td>$Pos</td>
    <td>$SKU</td>
    <td>$ProductName</td>
  </tr>
<% end_loop %>
</table>