Jump to:

23550 Posts in 19336 Topics by 2890 members

General Questions

SilverStripe Forums » General Questions » Special .ss Template for XML Version of Page

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: 1055 Views
  • DeklinKelly
    Avatar
    Community Member
    197 Posts

    Special .ss Template for XML Version of Page Link to this post

    HTML versions of page on my website can be accessed at URLs like this:
    /page/

    I also want each page to be available in XML format at URLs like this:
    /page.xml

    Now "Page.ss" is used as the HTML template.

    I want "PageXML.ss" should be used as the template for the XML version of pages.

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Special .ss Template for XML Version of Page Link to this post

    Shouldn't be too difficult.
    First you need a special rewrite-rule:

    <IfModule mod_rewrite.c>
       RewriteEngine On
       
       RewriteCond %{REQUEST_FILENAME} !-f
       RewriteRule (.*)\.xml$ sapphire/main.php?url=$1/xml [L,QSA]
       

       RewriteCond %{REQUEST_URI} ^(.*)$
       RewriteCond %{REQUEST_FILENAME} !-f
       RewriteRule .* sapphire/main.php?url=%1&%{QUERY_STRING} [L]
    </IfModule>

    The red part is what's newly added.

    Then add the following function to your Page_Controller:

    public function xml(){
       return array();
    }

    And now all that is left to do is to create a special template for that. You have to name it Page_xml.ss and put it in your templates folder.

    Calling /page.xml?flush=1 should now render your page with the XML template.

  • bunheng
    Avatar
    Community Member
    78 Posts

    Re: Special .ss Template for XML Version of Page Link to this post

    Hi Bana,

    I am sorry to disturb you, I was trying to generate the record from DataObject into XML format for using on my flash page.
    I have NewsItem as DataObject with following code:

    --------------------------------------------------------------
    NewsItem.php
    -------------------------------------------------------------
    class NewsItem extends DataObject {

    static $db = array(
    'Title' => 'Varchar(255)',
    'Details' => 'HTMLText',
    'Source' => 'Varchar(255)',
    'Date' => 'Date'
    );
    static $has_one = array(
    'NewsPage' => 'NewsPage',
    'NewsPhoto' => 'Image'
    );
    static $summary_fields = array(
    'Title' => 'Title',
    'Source' => 'Source',
    'Date' => 'Date'
    );

    public function getCMSFields() {
    return new FieldSet(
    new TextField('Title'),
    new SimpleTinyMCEField('Details', 'News Details'),
    new TextField('Source', 'News Source'),
    new DatePickerField('Date', 'Release Date'),
    new ImageField('NewsPhoto', 'Photo', Null, Null, Null, 'Uploads/latest-news/photos/')
    );
    }

    function caView() {
    return true;
    }

    //Return the Name as a menu title
    public function MenuTitle() {
    return $this->Title;
    }

    public function Link() {
    if ($NewsPage = $this->NewsPage()) {
    return $NewsPage->Link('show/') . $this->ID;
    }
    }

    public function LinkingMode() {
    if ($Controller = Controller::CurrentPage() && Controller::CurrentPage()->ClassName == 'NewsPage') {
    if (Controller::CurrentPage()->getAction() == 'show' && $NewsItem = Controller::CurrentPage()->getNewsItem()) {
    return ($NewsItem->ID == $this->ID) ? 'current' : 'link';
    }
    }
    }

    }
    ----------------------------

    my purpose i would like to generate all record from this DataObject into XML Format, you have any example, I am trying many way but it return 404 error.

    Your replies I really appreciated.

    Bunheng

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