Sitellite Design Templates
Chapter 5: Using Output Modes
A powerful part of Sitellite's concept of template sets is the modes.php file. The modes.php file allows you to control certain aspects of the different output modes offered by your template set.
An output mode is the file/data type of the content generated by a request to the Sitellite Content Server. For example, ordinary pages on the web are in HTML format, so the default output mode is html. It has a content/mime type of text/html. An alternate output mode might be WML, RSS, or RDF.
Alternate output modes are automatically added to your HTML output in the form of <meta rel="alternate" /> tags in the <head> block. This is done via XT's tag binding feature, which means you don't need any special tags in your HTML document for this to take place.
File Format
The modes.php file lives in the base folder of your template set and is an INI-formatted file, although it is recommended you edit it via the SiteTemplate "Edit Properties" form instead of modifying the file directly. This file contains INI "blocks" that each describe an output mode. You will find an example modes.php file in the default template set that comes pre-installed in new installations of Sitellite. This example contains the following:
; <?php /* [html] content_type = text/html filter 1 = "body: saf.Misc.Search" ;filter 2 = "body: siteglossary.Terms" ; */ ?>
The first and last lines of the file contents are standard INI security measures, which prevent the rest of the file contents from being read by visitors accessing the file directly through a web browser.
The [html] line denotes the start of the html output mode description block. A block ends when a new block starts, or at the end of the file.
The content_type value is the mime type of the output mode, sent to a visitor's browser in the Content-Type HTTP header.
The filter n lines are a numbered list of filters that can be applied to a given field before its output is passed to the XT templates that make up your template set. These can be used to add certain highlighting features to your content, or to format fields that need tweaking for a certain output mode. The field value begins with the name of the field, which can be any of the columns from the sitellite_page database table, or the special field value final which transforms the finished template output, followed by an SAF package name (separated by a colon).
The package references must include a function to transform the field named according to the following rules:
- The SAF path to the package is made lower-case and dots are replaced with underscores.
- The suffix _content_filter is appended to the above.
- The function must accept a single mixed value for the content field being transformed, and return a modified value of the same.
- Alternately, global functions can be specified instead, for example: filter 1 = "body: strip_tags"
Content Filters
Here is a simple content filter example function. You can try this out by saving it to a file named inc/app/myapp/lib/Example.php.
<?php
/**
* This is an example filter that inserts a trademark symbol next to
* any occurrence of the name "Sitellite".
*
* @access public
* @param string
* @return string
* @package Myapp
*/
function myapp_example_content_filter ($body) {
return str_replace (
'Sitellite',
'Sitellite<sup>TM</sup>',
$body
);
}
?>
To activate this filter for the body field, add the following line under the [html] block of your modes.php file:
filter 3 = "body: myapp.Example"
Creating an Alternate Output Mode: DocBook
First, we'll need to create the default template for our new output mode. Save the following DocBook markup to a file named docbook.default.tpl in your template set. In SiteTemplate, you can do this by entering "docbook" in the Output Mode field, and "default" in the Template Name field.
<xt:tpl version="1.0"><xt:xmldecl version="1.0" encoding="UTF-8" /> <xt:doctype root="book" access="public" name="-//OASIS//DTD DocBook XML V4.3//EN" uri="http://www.docbook.org/xml/4.3/docbookx.dtd" /> <article> <articleinfo> <title xt:content="head_title">Title</title> <abstract> <para xt:content="description">Description</para> </abstract> </articleinfo> <section> <title xt:content="title">Title</title> <para xt:replace="body">Body</para> </section> </article> </xt:tpl>
Next, we'll create an output filter to translate the HTML in the body field. Save this to the file inc/app/myapp/lib/DocBook.php
<?php
loader_import ('saf.HTML.Messy');
/**
* Converts various HTML tags to their DocBook equivalents. Please note that
* this list is far from complete.
*
* @access public
* @param string
* @return string
* @package Myapp
*/
function myapp_docbook_content_filter ($body) {
$m = new Messy;
$m->transform = array (
'p' => 'para',
'b' => 'strong',
'i' => 'emphasis',
'em' => 'emphasis',
'ul' => 'itemizedlist',
'li' => 'listitem',
'h1' => 'title',
'h2' => 'title',
'h3' => 'title',
'h4' => 'title',
'h5' => 'title',
'h6' => 'title',
);
$body = $m->clean ($body);
$body = strip_tags ($body, '<para><strong><emphasis><itemizedlist><listitem><title>');
return $body;
}
?>
Finally, we'll add the following new block to the modes.php file:
[docbook] content_type = "application/docbook+xml" filter 1 = "body: myapp.DocBook"
Our new output mode is now ready to be viewed. You can try it out by going to the following URL in your own Sitellite installation:
http://www.example.com/index/mode.docbook
Another Quick Output Mode: PDF
The PDF output mode is very easy to create, as the function necessary for HTML-to-PDF conversion is somewhat built-in. Sitellite provides a wrapper library in ext.htmldoc around the HTMLDoc HTML-to-PDF command line conversion too, available under a GPL license from:
The installation process for HTMLDoc itself should be straight-forward on most Unix systems, including Mac OS X:
tar -zxf htmldoc.tar.gz cd htmldoc ./configure make sudo make install
Now for the fun part. Creating a PDF output mode is as simple as creating the necessary pdf.default.tpl template (you can just copy your html.default.tpl for testing), and adding the following INI block to your modes.php file:
[pdf] content_type = application/pdf filter 1 = "final: ext.htmldoc"
You should now be able to view a PDF of your home page at:
http://www.example.com/index/mode.pdf
If you do run into problems, make sure that your command line htmldoc binary is somewhere in PHP's PATH environment variable. You will find useful information on the HTMLDoc web site about customizing the HTML of your template to control the PDF output. Note that where the HTMLDoc documentation says to put comments, use xt:note tags instead. For example:
<xt:note>HEADER LEFT "Site Name"</xt:note>




Charles Brunet