Jump to:

23013 Posts in 11540 Topics by 2825 members

General Questions

SilverStripe Forums » General Questions » renderWith nested arrays

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

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

Page: 1
Go to End
Author Topic: 1978 Views
  • k0m0r
    Avatar
    Community Member
    39 Posts

    renderWith nested arrays Link to this post

    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?

  • k0m0r
    Avatar
    Community Member
    39 Posts

    Re: renderWith nested arrays Link to this post

    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!

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