Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get child pages and loop through it's data on another page type?

Tags:

silverstripe

I have a TestimonalHolder page type that has Testimonials page type as its children which each have a Message $db field to store a testimonal.

Question is how do I access that $Message field on my HomePage.ss for example so I can loop through them and will put them into a slider etc.

Testimonials.php

class Testimonials extends Page {
    private static $db = array(
        'Message' => 'Text'
    );

    public function getCMSFields() {
        $fields = parent::getCMSFields();

        $fields->addFieldsToTab('Root.Testimonials', array(
            TextareaField::create('Message')
        ));

        return $fields;
    }
}

class Testimonials_Controller extends Page_Controller {

}

I know I can loop through them by using this code on my TestimonialHolder.ss page:

<% loop $Children %>
    <h2>$Title</h2>
    $Message
<% end_loop %>
like image 372
ifusion Avatar asked Mar 14 '23 08:03

ifusion


1 Answers

In your HomePage.php

public function getTestimonials($limit = 5) {
    return Testimonials::get()->limit($limit);
}

Then in your template, just use $Testimonials like you would $Children.

like image 126
ss23 Avatar answered Apr 28 '23 04:04

ss23