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

Cannot call page function when scope is not current page.


Go to End


2 Posts   3193 Views

Avatar
AnimalStripe

Community Member, 14 Posts

20 March 2015 at 12:17am

Edited: 20/03/2015 12:17am

All my page types extend Page. In Page.php I have the following function:

	function testMe() {
		return "Testing 123";
	}

In my template, I call it as follows:

$TestMe
<% loop $Parent.Children %>
	$TestMe
<% end_loop %>

However, it is not called from inside the loop even though the parent and all its children inherit from the Page class. Ugh!

I assume it has something to do with the current scope and that I need to put the function elsewhere. But I am unsure where it should go.

Any help would be appreciated.

Avatar
wmk

Community Member, 87 Posts

20 March 2015 at 12:29am

Hey AnimalStripe,

Page.php has two classes, Page and Page_Controller

In which class did you write the method testMe() ??

If it's in the Page (a DataObject) it should be callable inside the <% loop $Children %>, if it's in the current controller you cannot call it from the Dataobject directly.

If it's a general getter method (e.g. for getting something unrelated like a weather forecast) it's ok in the controller. Then you can call it using $Up.testMe or $Top.testMe, see templating docs

If it's a method that shoud return e.g. a formatted value of the current DataObject or a relation of the current DO be sure to place it in the DataObject subclass (in this case inside the Page class).