Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

Scope of controller vs page class


Go to End


4 Posts   1285 Views

Avatar
DesignCollective

Community Member, 66 Posts

21 September 2011 at 7:42am

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 :)

Avatar
Ryan M.

Community Member, 309 Posts

21 September 2011 at 11:13am

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

Avatar
martimiz

Forum Moderator, 1391 Posts

22 September 2011 at 1:24am

>> 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.

Avatar
DesignCollective

Community Member, 66 Posts

30 September 2011 at 12:25pm

Edited: 30/09/2011 12:26pm

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 :)