Jump to content

Archived

This topic is now archived and is closed to further replies.

Uriptical

Creating templates with XSL

Recommended Posts

Hi, I'm thinking up ideas for a CMS that I'm planning to build. One of the features I want to implement is a templating system, so that if in the future a designer with no experience of PHP wants to change the HTML and CSS of the page, he or she can do with relative ease.

The solution I have in mind is to create a bunch of XSL templates that the designer would be able to modify without touching any PHP code. PHP would get the appropriate data from the database, generate the XML based on this data, and then display it as XHTML, using something like this code (taken from the php manual):

[code]<?php

// Load the XML source
$xml = new DOMDocument;
$xml->load('collection.xml');

$xsl = new DOMDocument;
$xsl->load('collection.xsl');

// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules

echo $proc->transformToXML($xml);

?> [/code]

However, some of the information in my CMS will most likely be private, so I'm wondering if perhaps this isn't the best method to use, as the XML wouldn't be able to be saved on to the web server. Also, would it significantly slower converting it to XML first, and then to XHTML, especially since I don't really *need* the XML on it's own for anything.

Is this a good way of creating a templating system or not? Or would it be better to write my own templating engine from the ground up?

Share this post


Link to post
Share on other sites
The framework that I posted about yesterday ([url=http://phritz.fritz-works.com]Phritz[/url]) actually uses XSL templates for all of its output. You're right that it is a little slower than using a system such as Smarty, but I think that the benefits outweight the slight performance hit. The solution I used to help out with that was to avoid using the DOM implementations for the creation of the XML string. The assign() method works just like Smarty, creating indeces in an associative array instead of using $dom->createElement, which is way slower. Once you get to fetch() or display(), the XML string is created by traversing the array and manually building an XML document which is passed to the XSL processor and only then converted to a DOM object to be processed. When I benchmarked it against Smarty it was only behind a couple 1/1000 of a second, though that was on relatively simple page. On a more complex page rendering (say if you are processing a large number of templates for side blocks or something) it would probably show a bigger performance hit.
Feel free to download the source of Phritz and look at the template engine. Its GPL, so you can modify it and use it for your own if you like it.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.