Jump to:

3435 Posts in 1026 Topics by 866 members

Template Questions

SilverStripe Forums » Template Questions » [SOLVED] SS3 display content of array of DataObjects

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

Page: 1
Go to End
Author Topic: 252 Views
  • Stephan
    Avatar
    Community Member
    55 Posts

    [SOLVED] SS3 display content of array of DataObjects Link to this post

    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

  • Stephan
    Avatar
    Community Member
    55 Posts

    Re: [SOLVED] SS3 display content of array of DataObjects Link to this post

    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>

    252 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.