Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » How can I change the mime type of a template? or other headers?

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

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

Page: 1
Go to End
Author Topic: 1958 Views
  • nmac
    Avatar
    Community Member
    6 Posts

    How can I change the mime type of a template? or other headers? Link to this post

    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.

  • saimo
    Avatar
    Community Member
    67 Posts

    Re: How can I change the mime type of a template? or other headers? Link to this post

    I would be interested in this as well - subscribe.

  • nmac
    Avatar
    Community Member
    6 Posts

    Re: How can I change the mime type of a template? or other headers? Link to this post

    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.

  • nmac
    Avatar
    Community Member
    6 Posts

    Re: How can I change the mime type of a template? or other headers? Link to this post

    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;
        }
    }

    ?>

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