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.

Data Model Questions /

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

Passing an array to a template


Go to End


2 Posts   1209 Views

Avatar
tahbit

Community Member, 24 Posts

12 June 2014 at 4:16am

Guys - this is very simple - I have an array - see below. All I want to do is pass the data from the DB to a template and display it on the screen. Once I have understood how to do that I'll be away... The code below works fine in the controller - I can echo it - var_dump it you name it!!!

BUT I cannot get the stuff in a view - I just need someone to show me an example both in the controller and in the template.

<code>
public function getStudent()
{

$members = Student::get()->where("\"Name\" = 'Sarah Berens'");

foreach($members as $member)
{
$data = array('Name' => $member->Name);

}

var_dump($data);
//return $data;
//($members);
}

</code>

I have thrashed around for hours trying to do this - poking and hoping - all sorts. But no good - Please help!! Thanks

Avatar
tahbit

Community Member, 24 Posts

12 June 2014 at 6:25am

Right - cracked it! In case there is anybody else in a similar fix - here we go...

Controller:

$students = new ArrayList();

foreach($members as $member)
{

$students->push((new ArrayData(array( 
          'File' => $member->Name
          ))));
}

And for the view:


 <% loop Student %>
        <p>$File</p>
    <% end_loop %>

Can't believe it took me so long - but there we go... Hope it helps!