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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Rendering with default template?

Go to End

3 Posts   1007 Views


Community Member, 39 Posts

15 February 2012 at 6:43am

Edited: 15/02/2012 6:43am

How do I render a page on demand with its default template?
My code is:

public function onAfterWrite() {
$render = $this->renderWith(array($this->ClassName,'Page')));

Saving the page gives me the following error:

500//ERROR [User Warning]: None of these templates can be found in theme '':, IN POST /admin/EditForm Line 207 in /var/www/vhosts/

As you can see, current theme name variable is surprisingly empty, although I use set_current_theme() in config.php

All I need to do is export my page into a static file. But I am unable to get its published content.
What am I doing wrong?
Is there any method to render a page with its default template? Like $this->render() and not $this->renderWith()


Community Member, 39 Posts

15 February 2012 at 6:53am

For the future me:

I just found Director::test($this->Link())->getBody() but I don't consider it a kosher way to do this.


Forum Moderator, 1379 Posts

16 February 2012 at 12:36am

Edited: 16/02/2012 12:38am

I don't think you can render a page from within the Page class: OnAfterWrite() belongs to the DataObject (the Page). It's the Page_Controller that knows howto render itself...

So you should be able to do it like this (haven't tested it though):

$class = $this->ClassName . "_Controller";
$controller = new $class($this);
$render = $controller->renderWith(array($this->ClassName,'Page'))); 

Having said that, I think OnAfterWrite() might be called multiple times. So it might not be a good idea to do this - unless you at least cache $render.

As for Director::test - I think SilverStripe uses it in their Static caching - so you might as well, as long as it works - although normally 'test' would make me hesitate as well :-)