3217 Posts in 853 Topics by 812 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 347 Views |
-
Scope of controller vs page class

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
-
Re: Scope of controller vs page class

21 September 2011 at 11:13am
Both JoomlaPage() and Items() need to go in the Page/SiteTree class, not the controller.
-
Re: Scope of controller vs page class

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.
-
Re: Scope of controller vs page class

30 September 2011 at 12:25pm Last edited: 30 September 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
| 347 Views | ||
|
Page:
1
|
Go to Top |


