Jump to:

3392 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » Render DataObjectSet in template

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

Page: 1
Go to End
Author Topic: 1491 Views
  • okotoker
    Avatar
    Community Member
    39 Posts

    Render DataObjectSet in template Link to this post

    I am trying to take a set of two form fields that go into an array that then get saved into a session. I then want to take the array from the session on another page and put it into an ArrayData object that then goes into a DataObjectSet. I want to then render this into a table in my template. The amount of values in the array is dynamic. If the key were always the same I could see what to do, but with the unknown keys and values I am stuck.

    I have looked all around and haven't been able to get anything to work. So a simplified version of what I would like to do would be like this:

    //Page one
    $color = Session::get('Color');
    $number = Session::get('Number');
                
    $item = array($color=>$number);
    array_push($order,$item);

    Session::set('order',$order);

    //Page two
    function getItems() {
    $items = Session::get('order');
    $itemData = new DataObjectSet();
    $itemData->push( new ArrayData($items) );

    return $itemData;
    }

    //Template
    <% control getItems %>
    // I am not sure what values or methods I use here
    <tr><td>$key</td><td>$value</td></tr>
    <% end_control %>
    }


    So I may be way off base here, but this was the best I could figure out to do, seems difficult for an easy job. The concept I want here would be the same as taking form fields and grabbing them on another page through a POST then printing those values out on another page.

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Render DataObjectSet in template Link to this post

    $items is an array so you can pass that to DataObjectSet and it'll convert to an array data object. Using dynamic values for your keys would be slightly annoying.

    Something like this would be along the lines of how I would do it..

    $color = Session::get('Color');
    $number = Session::get('Number');

    $order[] = array('Color' => $color, 'Number' => $number);

    Session::set('order',$order);

    //Page two
    function getItems() {
    $items = Session::get('order');
    $itemData = new DataObjectSet($items);

    return $itemData;
    }

    <% control Items %>
    $Color
    $Number
    <% end_control %>

  • okotoker
    Avatar
    Community Member
    39 Posts

    Re: Render DataObjectSet in template Link to this post

    Thanks for the help, your suggestion did show me how to simplify what I am trying to do. I still can't get this to work for what I am trying to accomplish though. In this scenario Color's value would need to be an array to store multiple values that could have been submitted from the previous page. So 'Color'=>array('blue','yellow','red') and so on. I can get it to work if I just pass a single key => value but not with multiple values to loop over.

    Also, just for my further understanding of SS, why do I not need to put this into a <% control getItems %> like I would think I would to gain access to the values returned.

    Thanks again.

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Render DataObjectSet in template Link to this post

    Well then you would need to pass a nested DataObjectSet for the colours so that you can 'loop' over it.

    $itemData = new DataObjectSet();
    $itemData->push(new ArrayData(array(
    'Colors' => new DataObjectSet($colors)
    ));

    return $itemData;

    Also, just for my further understanding of SS, why do I not need to put this into a <% control getItems %> like I would think I would to gain access to the values returned.

    SS makes it 'nice' for developers by dropping the get part of any function in the template. So a function getFoo() would translate to $Foo in the template engine.

  • okotoker
    Avatar
    Community Member
    39 Posts

    Re: Render DataObjectSet in template Link to this post

    Ok, so I am just not getting this right,. Just to keep it simple for myself until I get this down, I have narrowed this down to just trying to get the output on a single page and manually putting in the options.

    So I have taken what you have given me and done this.

    function getItems() {
    $options = array('blue','green');
                
    $itemData = new DataObjectSet();
                
    $itemData->push(new ArrayData(array('Colors' => new DataObjectSet($options))));

    return $itemData;
    }


    Then I have tried a myriad of things in the templates with no luck. This setup gives me an error of "unknown class passed as parameter"

  • okotoker
    Avatar
    Community Member
    39 Posts

    Re: Render DataObjectSet in template Link to this post

    So I have picked through every bit of info I can and cannot figure out whats going on here. Any chance you could post just a small working example with the controller and template code? I have tried to use what you posted with no luck, I can't get the nested dataobjectset to loop.

    I think if I have something I can just drop into my site to see how it works and I can extrapolate it to anything I need. Plus if it doesn't work I know its something I have done incorrectly elsewhere.

    Thanks again for your help.

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Render DataObjectSet in template Link to this post

    In the template you need to use a key to output a value i.e $Color which translates to 'blue' or 'yellow' or 'red'.

    Inside your nested loop, Colors doesn't have keys for anything so of course, has no idea what to print (you could perhaps use $Me in this case, but that gets confusing).

    So starting with the html, nice and simple:

       <% control Items %>
          <% control Colors %>
             $Value
          <% end_control %>
       <% end_control %>

    Each item has a nested dataobjectset of colors that each have a $Value. My PHP looks something like follows. Note that I'm passing an array of arrays of colors and giving each a value. This will treat each color as it's own ArrayData object.

       public function getItems() {
          $items = new DataObjectSet();
          $items->push(new ArrayData(array(
             'Colors' => new DataObjectSet(array(
                array('Value' => 'blue'),
                array('Value' => 'yellow')
             ))
          )));

          return $items;
       }
    }

  • okotoker
    Avatar
    Community Member
    39 Posts

    Re: Render DataObjectSet in template Link to this post

    Thank you Willr for this! You clarified everything and I can now see exactly what I was doing wrong. I didn't understand that I need to:

    A- nest my control statements. This was a big confusion for me as I didn't see how I was supposed to access the nested arrays. Makes complete sense I just wasn't thinking about it correctly.
    B- I didn't get that I needed to pass an array of arrays as you said. Again makes sense and I messed around with this, but without my nested control statements I figured I had it wrong and moved on.

    So thanks again, now Monday I can finally wrap up this part of my project.

    1491 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.