Jump to:

23370 Posts in 18145 Topics by 2864 members

General Questions

SilverStripe Forums » General Questions » How do I use renderWith() outside a controller?

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: 398 Views
  • bxxxxx
    Avatar
    Community Member
    8 Posts

    How do I use renderWith() outside a controller? Link to this post

    Hi,

    I have a file containing MyPage extends Page, and MyPage_Controller extends Page_Controller.

    Now I want *onBeforePublish* trigger some filewriting (xml-file to be exported via ftp).

    I tried:

    function onBeforePublish(){

    $xml = $this->renderWith('XML');
    file_put_contents('/full/path/to/xml-file.xml', $xml);

    }

    Result:

    ERROR [User Warning]: None of these templates can be found in theme 'xyz': XML.ss
    IN POST /admin/pages/edit/EditForm

    Next, I tried:

    class MyPage extends Page
    {

    ...

    function onBeforePublish(){

    $myPageController = new MyPage_Controller();
    $myPageController->setDatacord($this);
    $xml = $myPageController->xml();
    file_put_contents('/full/path/to/xml-file.xml', $xml);
    }

    }

    class myPage_Controller extends Page_Controller{

    private static $allowed_actions = array (
    'xml'
    );

    public function setDatacord($data){
    $this->dataRecord = $data;
    }

    public function xml(){
    $xml = $this->renderWith('XML');
    return $xml;
    }

    }

    ....

    Same Result, with a backtrace to the xml-action in MyPage_Controller.

    Remark:

    The MyPage-Object is only viewable to logged in users and therefore my third attempt:

    function onBeforePublish(){

    $request = Director::test($myPageURL.'xml');
    $xml = $request->getBody();
    ...

    }

    fails due to a redirect to the Login-Form.

    Any ideas how to solve this?

    Btw:

    The template XML.ss is in the themes/xyz/templates/includes folder.

  • Bambii7
    Avatar
    Community Member
    254 Posts

    Re: How do I use renderWith() outside a controller? Link to this post

    Your first function is correct. Add parent::onBeforePublish() in there.

    function onBeforePublish(){
    parent::onBeforePublish();
    $xml = $this->renderWith('XML');
    file_put_contents('/full/path/to/xml-file.xml', $xml);

    }

    It should be in the MyPage model. The only issue is 'XML' its looking for a template in the template dir called 'XML.ss'.

    Perhaps make a new template call xmlexport.ss inside there you'll need to structure the nodes as you see fit. Then call renderWith('xmlexport')

    <!xml>
    <% loop stuff %>
    $Title
    <% end_loop %>

    Depending on what you're after you could just serialise the object to xml and write that out http://stackoverflow.com/questions/137021/php-object-as-xml-document

  • bxxxxx
    Avatar
    Community Member
    8 Posts

    Re: How do I use renderWith() outside a controller? Link to this post

    No, I assume there is no

    parent::onBeforePublish();

    The result is:

    ERROR [User Error]: Uncaught Exception: Object->__call(): the method 'onbeforepublish' does not exist on 'MyPage'
    IN POST /admin/pages/edit/EditForm
    Line 759 in /Users/htdocs/framework/core/Object.php

    See:
    http://www.silverstripe.org/general-questions/show/18755

    My workaround meanwhile is to use a native cUrl-request to grab the xml via the controller action.

    I removed the *login-restriction* for the xml action.

    Although I am really interested, if it is possible to generate a *templated* output string without using a http request from the model.

    The *XML* naming is manually integrated for the discussion here. In fact *MyPage* is not called *MyPage* and *XML* is really *MyXML*

    ;-)

  • bxxxxx
    Avatar
    Community Member
    8 Posts

    Re: How do I use renderWith() outside a controller? Link to this post

    The solution was in my case to move the MyXML.ss to the /mysite/templates directory away from any themes folder.

    Now the renderWith works fine in onBeforePublish().

    In my opinion the issue is solved, thank you very much for your help.

    398 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.