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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Special .ss Template for XML Version of Page


Go to End


3 Posts   1648 Views

Avatar
DeklinKelly

Community Member, 197 Posts

24 August 2010 at 1:27pm

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.

Avatar
bummzack

Community Member, 904 Posts

25 August 2010 at 6:48pm

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.

Avatar
bunheng

Community Member, 78 Posts

14 June 2011 at 12:37am

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