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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

[SOLVED] SS3 display content of array of DataObjects


Go to End


2 Posts   1827 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>