phpsilverstripe

Output to template, which variable to use?


I have a template which needs to display users that have been assigned to that page via GridField.

When a user is assigned to the page their ID is stored, I am then retrieving that info like so

 class Shows_Controller extends Page_Controller {

    # Get key people from ShowsContact class / input via ShowsContact GridField
    public function getKeyPeople(){

        if($this->ShowsContacts()->exists()){

            $result = array();

            foreach($this->ShowsContacts()->column('MemberID') as $teamMemberID){
                 array_push($result, Member::get()->byID($teamMemberID)->Nickname);
            }

            var_dump($result); # Using var_dump here to see if I get what I need...
       }
    }
 }

I have tried using SilverStripes ArrayList but couldn't get that to work either - here's the same thing but using ArrayList...

 $result = new ArrayList();

 foreach($this->ShowsContacts()->column('MemberID') as $teamMemberID){
    $result->add(Member::get()->byID($teamMemberID)->Nickname);
 }

 var_dump($result);

The output of this is what I'm expecting but I don't understand how to loop through the results and what variable I need to use so that the loop displays the results within my template..?


The result from the var_dump is this, I need to get those names out to the relevant part of the webpage

 array(3) {
    [0]=>
    string(6) "vlad-s"
    [1]=>
    string(10) "lawrence-r"
    [2]=>
    string(8) "darren-g"
 }

I've also made attempts at using <% with %> but that didn't work either


Edit

Just to mention, I'm only trying to obtain the usernames at the moment but will need to get multiple things from the ID such as Firstname, Surname, ImageURL


Solution

  • Wow finally got it working how I needed it to!

    What I was missing was using ArrayData() when trying to add to the ArrayList()

    Here's the working code

     class Shows_Controller extends Page_Controller {
    
        # Get key people from ShowsContact class // input via ShowsContact GridField
        public function getKeyPeople(){
    
            if($this->ShowsContacts()->exists()){
    
                $result = new ArrayList();
    
                foreach($this->ShowsContacts()->column('MemberID') as $teamMemberID){
                    $result->add(new ArrayData(array(
                        'Nickname' =>Member::get()->byID($teamMemberID)->Nickname
                        )
                    ));
                }
    
                return $result;
            }
        }
     }
    

    And now in my template I can use

     <% loop KeyPeople %>
         $Nickname
     <% end_loop %>
    

    To get the data out where I need it!