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, biapar, Willr, Ingo, swaiba, simon_w

How do I use renderWith() outside a controller?


Reply


4 Posts   510 Views

Avatar
bxxxxx

Community Member, 8 Posts

18 December 2013 at 4:22am

Edited: 18/12/2013 4:24am

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.

Avatar
Bambii7

Community Member, 254 Posts

18 December 2013 at 3:22pm

Edited: 18/12/2013 3:23pm

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

Avatar
bxxxxx

Community Member, 8 Posts

19 December 2013 at 1:04am

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:
[url]http://www.silverstripe.org/general-questions/show/18755[/url]

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*

;-)

Avatar
bxxxxx

Community Member, 8 Posts

19 December 2013 at 3:29am

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.