How to get child pages and view data on another page type?

I have a page type TestimonalHolderthat has a page type Testimonialsas its children, each of which has a Message$ db field to store testimonal.

The question is how can I access this field $Messageon mine HomePage.ss, for example, so that I can skip them and put them in 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 that I can skip them using this code on my page TestimonialHolder.ss:

<% loop $Children %>
    <h2>$Title</h2>
    $Message
<% end_loop %>
+4
source share
2 answers

In your HomePage.php

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

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

+7
source

In your template:

<% loop $Testimonials('BlogPost').Limit(5) %>
    $Title
<% end_loop %>

$List ( Filter(), Sort() Limit()) , MVC.

!

: http://www.silverstrip.es/blog/retrieving-some-dataobjects-in-template-without-php-getter/ ( : )

0

Source: https://habr.com/ru/post/1613149/


All Articles