Jump to:

3439 Posts in 1028 Topics by 869 members

Template Questions

SilverStripe Forums » Template Questions » Scope of controller vs page class

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

Page: 1
Go to End
Author Topic: 512 Views
  • KINKCreative
    Avatar
    Community Member
    56 Posts

    Scope of controller vs page class Link to this post

    Hi guys, a question - in order to prevent any mistakes design/coding.

    I noticed that when I create methods to be displayed in templates, they sometimes appeared in the template, and sometimes not. I noticed that if they are present in the page class itself, it is fine. If a method is present in the controller, sometimes they appear and sometimes they don't (at least that's my observation so far).

    Is it an issue if I am accessing a method of a class that I passed to the template via a DataObject::get clause (so not the currently running controller, of the page viewed). E.g. I have an aggregator page that parses Joomla articles. I reference this one in the controller of the HomePage.php class like this:

    ...
    public function JoomlaPage() {
       $joomlaPage=DataObject::get("JoomlaAggregatorPage","URLSegment='joomla-aggregator'");
       return $joomlaPage;
    }
    ...

    In the JoomlaAggregatorPage.php in the class JoomlaAggregatorPage_Controller I have:

    ...

    public function Items($limit = 10) {
    ...
    }

    ...

    In the HomePage.ss template I then do:

    <% control JoomlaPage.Items %>
    $properties_of_items
    <% end_control %>

    I noticed I can access the properties of the page itself but the control block doesn't return anything. Scoping problem?

    Any insight on this would be amazing

  • Ryan M.
    Avatar
    Community Member
    309 Posts

    Re: Scope of controller vs page class Link to this post

    Both JoomlaPage() and Items() need to go in the Page/SiteTree class, not the controller.

  • martimiz
    Avatar
    Forum Moderator
    1078 Posts

    Re: Scope of controller vs page class Link to this post

    >> If a method is present in the controller, sometimes they appear
    >> and sometimes they don't (at least that's my observation so far).

    There is a logic to that: the current Page_Controller takes care of displaying the current page. That's why the page controller methods are available in the Template.

    However, once you enter a <% control %> ... <% end_control %> structure, you deal with DataObjects. That goes as well for pages that are returned by Children(), Menu() or DataObject::get() methods: they are Page objects - NOT Page_Controllers, so you can only access their Page methods.

  • KINKCreative
    Avatar
    Community Member
    56 Posts

    Re: Scope of controller vs page class Link to this post

    Okay thanks for the response! Now, a question that follows - is there a way for a page class to access a method in the page_controller class? E.g. if I create class methods that need a method that is in the controller, is there a reference that I can use to access them or would I need to duplicate the methods into the controller?

    Thank you

    512 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.