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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

renderWith nested arrays


Go to End


2 Posts   3125 Views

Avatar
k0m0r

Community Member, 39 Posts

20 April 2010 at 8:29pm

Hi.
I'm trying to render an Ajax response with a specified template.

My controller code looks like this:

$data = array(
'Items' => array( 'Test1', 'Test2', 'Test3' ),
'Pairs' => array( 'Foo' => 'Bar' ),
);
return $this->customise($data)->renderWith('AjaxResponse');

Now, in the template, I can use the control Items to get the iterator:

<% control Items %>
<item />
<% end_control %>

So it prints three separate items, but I am unable to get their contents.
How can I print out 'Test1','Test2','Test3' in Items control?
I also tried to use $Pairs.Foo, but it still didn't work.
So I've got two questions:

1. How should I construct the $data array to use it as controls in the template?
2. Is it possible to get the keys, not only values, from a control?

Avatar
k0m0r

Community Member, 39 Posts

22 April 2010 at 2:50am

Edited: 22/04/2010 2:53am

Works perfect, thanks a lot!
And for getting the keys, not only values, from a control - I changed it to:

foreach($arr as $key => $val) {
$data->push(new ArrayData( array( 'Key' => $key, 'Value' => $val ) ));
}

So now I can use:

<% control Items %>
<item id="$Key"><![CDATA[$Value]]></item>
<% end_control %>

Thank you very much!