4

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 %>
ifusion
  • 2,163
  • 6
  • 24
  • 44

2 Answers2

7

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.

ss23
  • 148
  • 10
0

In your template:

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

With this $List helper you can access a simple datalist (even with Filter(), Sort() and Limit()) directly in your template, although this violates pure MVC a bit.

So handle with care!

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

wmk
  • 4,598
  • 1
  • 20
  • 37