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

Render DataObjectSet in template


Go to End


8 Posts   3093 Views

Avatar
okotoker

Community Member, 50 Posts

21 September 2011 at 11:39am

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.

Avatar
Willr

Forum Moderator, 5523 Posts

21 September 2011 at 5:02pm

$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 %>

Avatar
okotoker

Community Member, 50 Posts

23 September 2011 at 6:25am

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.

Avatar
Willr

Forum Moderator, 5523 Posts

23 September 2011 at 9:00am

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.

Avatar
okotoker

Community Member, 50 Posts

23 September 2011 at 10:09am

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"

Avatar
okotoker

Community Member, 50 Posts

24 September 2011 at 9:59am

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.

Avatar
Willr

Forum Moderator, 5523 Posts

24 September 2011 at 12:01pm

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;
	}
}

Avatar
okotoker

Community Member, 50 Posts

24 September 2011 at 12:30pm

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.