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.

Archive /

Our old forums are still available as a read-only archive.

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

How can I change the mime type of a template? or other headers?


Go to End


4 Posts   2906 Views

Avatar
nmac

Community Member, 6 Posts

24 May 2008 at 1:01am

Edited: 24/05/2008 1:02am

hi Folks,
I just discovered SS today, and well, "impressed" doesn't even come close.
Six hours later and I am deep into building a site...
My slight problem now is I have a model/controller for a new Page type... lets call it XMLPage... and a corresponding View... e.g. /template/XMLPage.ss ...
I want to send the output of the view with an xml mime-type..
How can I override the text/html mimetype currently being sent?
I'm familiar with the PHP header function - I just dont see if a) there is a control statement I can use in a template file, or b) a function call in a controller... any help very appreciated!
Cheers,

N.

Avatar
saimo

Community Member, 67 Posts

24 May 2008 at 10:26am

I would be interested in this as well - subscribe.

Avatar
nmac

Community Member, 6 Posts

26 May 2008 at 9:27am

Well some progress at this end...
In the controller... use the index method..

class XMLPage_Controller extends ContentController {
	function index() {
		header("Content-Type: application/xml");
		$out = $this->renderWith("XMLPage");
		print $out;
		exit;
	 }

}

with a template XMLPage

$XML

and a model along the lines of

class XMLPage extends SiteTree {
	static $db = array(
	   'XML' => 'Text'
	);
	static $has_one = array(
	);

	static $icon = "mysite/images/treeicons/xml";

	
	function getCMSFields() {
		$fields = parent::getCMSFields();
		$fields->removeFieldFromTab('Root.Content.Main', 'MenuTitle');
		$fields->removeFieldFromTab('Root.Content.Main', 'Content');
		$fields->removeFieldFromTab('Root.Content.Metadata', 'MetaTitle');
		$fields->removeFieldFromTab('Root.Content.Metadata', 'MetaDescription');
		$fields->removeFieldFromTab('Root.Content.Metadata', 'MetaKeywords');
		$fields->removeFieldFromTab('Root.Content.Metadata', 'ExtraMeta');

		
		$fields->addFieldToTab('Root.Content.Main', new TextareaField(
			$name = "XML",
			$title = "XML of the core data file",
			$rows = 30,
			$cols = 60,
			$value = "<xml>",
			$dontEscape = TRUE), 'Content');
	   return $fields;
	}

}

There is some progress in that the controller now sends the right header, followed by the content of the XML - but in trying to use the inherited variable $dontEscape, I am obviously missing something, as the content of the XML field IS escaped.
Hmm.
N.

Avatar
nmac

Community Member, 6 Posts

26 May 2008 at 9:54am

ok, well, this works for me, not elegant, but it works. If there is a better way, please let me know.
My requirement was to have a custom form in the CMS, that allows a title, and a textarea field that will contain XML, then on the public side of the site, request the page and get an XML file, with valid headers for a content type of xml.
In my previous post I made some headway using the index function of the controller to render the view and print it out, but escaping was getting in the way.
I found that if I dispense with the view altogether, as in my case, it serves little purpose anyway, a simple echo of the field works - it does not get escaped.
FWIW..

<?php
class XMLPage extends SiteTree {
	static $db = array(
	   'XML' => 'Text'
	);
	static $has_one = array(
	);

	static $icon = "mysite/images/treeicons/www";

	function getCMSFields() {
		$fields = parent::getCMSFields();
		$fields->removeFieldFromTab('Root.Content.Main', 'MenuTitle');
		$fields->removeFieldFromTab('Root.Content.Main', 'Content');
		$fields->removeFieldFromTab('Root.Content.Metadata', 'MetaTitle');
		$fields->removeFieldFromTab('Root.Content.Metadata', 'MetaDescription');
		$fields->removeFieldFromTab('Root.Content.Metadata', 'MetaKeywords');
		$fields->removeFieldFromTab('Root.Content.Metadata', 'ExtraMeta');

		$fields->addFieldToTab('Root.Content.Main', new TextareaField(
			$name = "XML",
			$title = "XML Data",
			$rows = 30,
			$cols = 60,
			$value = "<?xml version=\"1.0\"?>
			<!-- Put your XML below -->
			<sample>Some Data</sample>"), 'Content');
	   return $fields;
	}

}
 
class XMLPage_Controller extends ContentController {

	function init (){
		parent::init();
	}

	function index() {
		header("Content-Type: application/xml");
		echo $this->XML;
		exit;
	 }
}
 
?>