Using XSLT as a template engine, a la smarty, is something i've been screwing around with alot lately. While my experience with smarty is pretty shallow, I think XSLT can handle most of the basic presentation type functionality. variable substitution, iterating over a set of data, conditional statements, etc...and having it as a well defined, cross-platform technology is a nice little fact as well.
As seratonin has suggested, using the DomDocument to create your own models (assuming an MVC arch.) makes it very easy to populate the model with valid XML markup, similar to the way smarty "assigns" PHP variables to a smarty object.
As far as the overall structure of the site goes *takes a deep breath*, I've been mulling over different ways to fit all this stuff together, mainly with regard to the presentation. Until a few months ago (when I decided to actually start learning XSL), I stuck to the "header-content-footer" method of building pages that everyone in the world uses:
about.php
PHP Code:
<?php require('header.inc.php'); ?>
...about cholmon...
<?php require('footer.inc.php'); ?>
The thing that drew me to XSL, however, was the fact that there are few things I hate more than writing a bunch of PHP/HTML/PHP/HTML/PHP to display a giant result set from MySQL. Even when I use CSS to simplify the HTML that needs to be generated, it still gets on my nerves. Other things, like conditionally include()ing or require()ing ANYTHING made me giddy when I realized how much easier XML/XSLT can make the whole process. I don't know why I waited so long to try to learn this stuff.
Anyway, I started loading all my data into DomDocuments, then did the transformation, and all of the sudden XSLT was my new friend...until I realized that I was writing a single stylesheet for each page. How was I supposed to include a common header and footer for each page? After finding the <xsl:include> tag, I changed the structure of the page from having about.php just transform the DomDocument with about.xsl to display the output, to having about.xsl first include main.xsl for all the header and footer stuff:
main.xsl
Code:
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="content"/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
about.xsl
Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:include href="main.xsl" />
<xsl:template match="content">
all about......something
</xsl:template>
</xsl:stylesheet>
This way I could just include main.xsl at the top of each stylesheet, and they'd all have the same outer structure. Obviously both main.xsl and about.xsl have much more stuff in them, and main.xsl assumes a relatively basic overall page structure (ie, a title/logo bar, a navigation menu, a content section, etc). Those page elements, hoewver, can be shaped however I want, and placed wherever I want...as long as each page in the site can fit into that basic structure.
as far as the XML that gets parsed through these templates, I'm still refining the way it's structured. I don't have any kind of formal schema, but it is essentially built like this (in memory):
$xml (DomDocument object)
Code:
<?xml version="1.0" ?>
<page>
<title/>
<stylesheets/>
<scripts />
<navigation>
<nav id="home" link="index.php">Home</nav>
<nav id="about" link="about.php">About</nav>
<nav id="contact" link="contact.php">Contact</nav>
</navigation>
<content/>
</page>
Actually, since every page will share some of the same elements, like the nav options, I keep this XML in an actual file, called core.xml. when a page like about.php is hit, it starts by doing whatever set up stuff it needs to...like hitting the DB for content. Then it loads core.xml into a DomDocument, and adds child elements to elements like <title> and <content>. FOr example, once about.php is ready to send the XML through XSLT to display the page, the XML is modified to look like:
$xml (DomDocument)
Code:
<?xml version="1.0" ?>
<page>
<title>About Cholmon</title>
<stylesheets>
<css>stylesheet.css</css>
</stylesheets>
<scripts />
<navigation active="about">
<nav id="home" link="index.php">Home</nav>
<nav id="about" link="about.php">About</nav>
<nav id="contact" link="contact.php">Contact</nav>
</navigation>
<content>
<paragraph>
blah blah blah blah blah blah blah blah blah blah
</paragraph>
<paragraph>
yackity schmackity yackity schmackity yackity schmackity
</paragraph>
</content>
</page>
Notice how <navigation> became <navigation active="about">...that lets the 'About' link like show up as the active link. How that is actually done (CSS?) depends on how main.xsl is told to parse the <nav> elements.
Also, the <content> element of core.xml can end up containing anything I want, in this case some <paragraph> elements...as long as about.xsl has a template to handle <paragraph> elements.
crap that's way more than I intended to write. this whole idea o' mine is obviously still in infancy, but there are a few things I have in mind to make things a little bit smoother/faster. Caching the DomDocument, for instance, would help. I've also given some thought to using SimpleXML instead of DomDocument in order to simplify adding and changing elements that are loaded from core.xml...any ideas on that front?
Questions and comments are much appreciated...I haven't really gotten anyone else's opinions on all this stuff yet.
Bookmarks