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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Template Questions /

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

access controller functions inside <% control AllChildren %>

Go to End

16 Posts   6992 Views


Community Member, 4 Posts

8 April 2010 at 12:22am

I was just about to come here and post the solution I came up with. While it's probably a little bit of a dirty hack, it seems to work just fine in this case. What I did was create a method inside the model that I can then access via a control. Here's my method:

	public function ContactForm(){
		$cf = new ContactPage_Controller;
		return $cf->ContactForm();

I put this inside the model (duplicate method name as the one inside the controller, so perhaps a bit confusing). To access this, I use the same means as I had originally been attempting with the controller method:

	<% control Page(contact) %>
		<% control ContactForm %>
		<% end_control %>
	<% end_control %>

The form seems to work just fine, including any error handling and success pages.


Community Member, 72 Posts

22 February 2011 at 12:48pm

I have a similar issue where I'm pulling in AllChildren into a holder page. Basically my holder page is a ContactHolderPage and all of its sub pages are contact pages(ContactPage) and forms for different departments for "Company X". So each page needs to have a slightly different form on it and it's form is mailed to someone different.

I used BradPlunkett's little hack and it pulls in the form, but its the same form for all the pages, which is expected as I only have have one ContactForm() function in ContactPage.php.

Is there a way to have the ContactForm() function know what page (child page) its on and then add in some variable or name to make each form different?

I've tried adding in $this->URLSegment to the form name, but when I add it to the allowed_actions array, it throws the following error: Parse error: syntax error, unexpected T_VARIABLE, expecting ')' in /my/web/location

Any help here is appreciated. Thanks


Community Member, 89 Posts

22 July 2011 at 6:17am

I too am looking for help with this.

My pages have a function that returns yes if the current user can view the page.

It works great in the standard template, as if they don't have access I just display a message in place of the content.

However I would like an indication of the pages a user doesn't have access to in the site menu. But because I cannot access the function unless I'm on that actual page, I'm not sure how to do this.

Help appreciated.


Community Member, 545 Posts

22 July 2011 at 7:14am

Moloko_man try taking the Form off of the PageType and making new Form type by extending the form object

Since the Form is called inside a different controller, in theory, it should return it with enclosing controller instead of the page controller.

Purplespider what does your permission check look like? As long as its called from within that child block you should be able to pull the child data and do an if filter

<% control AllChildren %>
<% if CanView %><a href="$Link">$Title</a><% else %>$Title<% end_if %>
<% end_if %>


Community Member, 89 Posts

22 July 2011 at 9:17pm

That's not working for me. In my Page_Controller I have:

function isLocked() {		
		if ($this->canView() == "yes") {
			return false;
			return true;

If I put this into a template outside of a control, for example in the page title tag, it correctly returns Locked if the page is locked:

<% if isLocked == true %>Locked<% else %>Unlocked<% end_if %>

However, if I try to use it in a control, for example:

<% control Menu(1) %>
<a href="$Link">$MenuTitle <% if isLocked == true %>Locked<% else %>Unlocked<% end_if %></a>
<% end_control %>

It doesn't work, and just always displays "Unlocked".

Any ideas?


Forum Moderator, 1391 Posts

23 July 2011 at 1:23am

Try putting the function in your Page class instead of in the Page_Controller...


Community Member, 89 Posts

23 July 2011 at 2:33am

Now that works thanks. But my function, relies on another function, that relies on another function. What's the difference between putting a function in the Controller or in the Model?


Forum Moderator, 1391 Posts

23 July 2011 at 4:38am

As I understand it: the Page_Controller controls the output (i.e. the template) for the current Page. That's why all Page_Controller functions for the current page are accessible from the template.

However, within a <% control %> group, the scope switches to whatever DataObjectSet you're traversing. Menu(), Level(), Children() are all DataObjecSets, made up of 'ordinary' DataObjects, or Page objects in this case.

And from within a Page Object you just cannot access Page_Controller methods. So you need to define your functions within the Page class. In some cases it might work to instantiate the Page_Controller from within a function in the Page class, and then access Page_Controller functions.

To make things confusing: the other way around, it seems the Page_Controller can access Page class methods and properties... :-)

Go to Top