January 10th, 2007
I don’t like the idea of a meta-PHP templating language: PHP is a templating language. However my attempt to use the natural templating features of PHP was abysmal. Thinking there must be something better I started playing around with generating XML and transforming that into HTML using XSL. I’m not pretending this post is a work of art, it’s just my preliminary experiment using the technologies.
First up you’ll probably need to install XSL functionality for your PHP install. You can do this easily on Ubuntu with the following:
sudo aptitude install php5-xsl
W3Schools describes XSL as a combination of the following:
Basically the main functionality I have been exposed to is XSLT, which is implemented as a class in PHP that accepts an XML document and a XSL stylesheet.
To generate the XML I’m using the PHP DOM class as I envisage needing random access to the nodes, but you could use the serial XMLWriter class.
$xml = new DomDocument('1.0'); $article = $xml->appendChild($xml->createElement('article')); $article->setAttribute('id', 1); $title = $article->appendChild($xml->createElement('title')); $title->appendChild($xml->createTextNode('This is a title')); $body = $article->appendChild($xml->createElement('body')); $body->appendChild($xml->createTextNode('This is some body text'));
This will create the following XML:
<article id="1"> <title>This is a title</title> <body>This is some body text</body> </article>
Next you need a XSL template. I’ve written this inline so you can basically copy everything in this post into one file, but practically you can have this stylesheet as an external file and load it that way. There’s a nice table of fundamental XSL elements in a Zend article. I’m going to study XSL closer, but for the sake of example here’s what I cobbled together:
$xsl_template=< <<EOD
<xml version='1.0'>
<xsl :stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
</xsl><xsl :template match="article">
<html>
<head><title><xsl :value-of select="title" /></title></head>
<h1><xsl :value-of select="title" /></h1>
<body>
<xsl :value-of select="body" />
</body>
</html>
</xsl>
EOD;
Now you’ve got your XML and XSL file, you can use the XSLTProcessor class to transform it to your HTML:
$xsl = new DomDocument(); $xsl->loadXML($xsl_template); $proc = new XSLTProcessor(); $proc->importStylesheet($xsl); echo $proc->transformToXML($xml);
The real power lies in your XSL templates. Once you have your base XML file, you can transform it to any XML-like file you wish, which would include RSS.
This post is a bit of a brain-dump, so here are the links I’ve been working from:
Tony Marston is very keen on the XSL transformation approach to separating logic from content, here are the two articles I’ve found most helpful:
January 10th, 2007 at 11:38 pm
Not to go against the grain of the post or anything, but Eragon isn’t really a rip-off of LOTR… if it were, I’d be sending angry-person letters to the author, lol. The book was great and if I could come in human contact with you I’d make you read it, but the movie teh sucked. ^^;
Go keep enjoyin’ your plays an England winterness… yupyup
January 11th, 2007 at 9:58 pm
I do check back on your blog to see if you’ve replied y’know.
The book was written by a teenager, I believe?
April 3rd, 2007 at 9:05 pm
[…] the PHP XSL functions I’ve been implementing a custom form markup language that decouples awkward form validation […]