Jump to:

23000 Posts in 11854 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » Rendering with default template?

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

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

Page: 1
Go to End
Author Topic: 522 Views
  • k0m0r
    Avatar
    Community Member
    39 Posts

    Rendering with default template? Link to this post

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

    public function onAfterWrite() {
    parent::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 '': MainPage.ss, Page.ss IN POST /admin/EditForm Line 207 in /var/www/vhosts/orange.czterystacztery.pl/htdocs/sapphire/core/SSViewer.php

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

  • k0m0r
    Avatar
    Community Member
    39 Posts

    Re: Rendering with default template? Link to this post

    For the future me:

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

  • martimiz
    Avatar
    Forum Moderator
    1038 Posts

    Re: Rendering with default template? Link to this post

    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

    522 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.