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

How does one output a template to a document


Go to End


5 Posts   1518 Views

Avatar
Sh33pDawgShep

Community Member, 10 Posts

9 March 2016 at 5:33pm

Edited: 09/03/2016 8:44pm

I have a template successfully generating the XML data but to a webpage; how do I write this to a file?

Avatar
Friizu

Community Member, 17 Posts

9 March 2016 at 9:03pm

Edited: 09/03/2016 9:14pm

Hei

As general you should be able to do something like this:

	$test = $this->renderWith('template');
	file_put_contents(ASSETS_PATH .'/xml.xml', $test);

Take a look here, more talk and examples how to make this work:
http://www.silverstripe.org/community/forums/dataobjectmanager-module-forum/show/16191?start=0

Above is ment if you need to write a file, if u just need output of rss/xml you can use different approach
https://docs.silverstripe.org/en/3.2/developer_guides/integration/rssfeed/

Avatar
Sh33pDawgShep

Community Member, 10 Posts

9 March 2016 at 9:19pm

Hi

Thank you for taking the time to reply.

I'm currently using a template which triggers a few RESTful calls and generates the data in the XML template. There is nothing written to the database and so I'm having difficulties working out how to properly implement the code you cite below. i.e. onAfterWrite event handler is not triggered.

Please forgive my naïvety here as I'm still learning.

Avatar
Friizu

Community Member, 17 Posts

9 March 2016 at 11:07pm

Hei

Yes this seems a bit tricky, but i wonder if u can make Director rule for xml route example something like:

Director:
  rules:
    'xmlpath': 'XmlController'

My idea is that u need to check when your xml is requested and then run controller to generate xml. Maybe there is some better way to do this, anyway u need to check somehow when your xml is requested ...

Avatar
Sh33pDawgShep

Community Member, 10 Posts

10 March 2016 at 7:41pm

Hi

Both of those posts helped immensely. Thank you.

I have ended up with a successful output of formatted XML using the Director rule and procedure as per Friizu's suggestions; so that I may pass a URL to my installation and it'll output an XML based on the parameters set on that URL. Quite satisfying. Now for all the validation, security and error handling!

I did have a bunch of re-coding to do because I could not see how one could have calls to different methods from within the template if one is sending the data to the template using renderWith not without creating a separate class in the manner of a Page Controller. But this is a minor aside for the most part.

Thank you again.