I love PHP, but I spend way too much of my time trying to come up with ways to separate content from presentation. My attempts to do this always end up being the messiest (and, to me, most embarrassing) parts of my code.
Template engines like Smarty claim to solve this problem, but the logic of this claim is beyond me. Every PHP template engine I’ve seen has merely added more complexity to the situation, and none of them actually solve the core problem, which is to keep content pure and free from any kind of presentation or program logic. Sure, creating a template language that reproduces PHP language constructs is a great way to keep content free of PHP code, but how does this help when you’re merely replacing the PHP code with something even more confusing that does the same thing? Not surprisingly, I’ve always resisted using template engines.
A few weeks ago, I fell in love with XSLT. Assuming your content is XML, you can use XSLT to make it look however you want. The content layer remains completely separate from the presentation layer, and the flexibility XSLT provides is unsurpassed. I began wondering how feasible it would be to have my PHP applications generate XML content, then use XSLT templates to transform the XML into HTML. PHP already has an XSLT extension, so implementing this wouldn’t be hard at all. Plus, since they’d be generating all their content as XML, my applications would have the added benefit of being able to provide raw XML feeds for everything they do.
However, I’m troubled by the fact that there are very few existing PHP applications that actually use this solution. I’m hardly the first person to have thought of it, so I had expected it to be fairly widely used already, but this is not the case. Based on Google searches, it seems that many PHP developers think XSLT adds too much complexity to be useful (which is exactly the same argument I have against other template solutions). People are also wary of the processing overhead of XSLT, which is a valid concern, although a simple caching solution will take care of that.
I’m uncomfortable being an early adopter. I’m worried that I’m missing some glaring problem that will eventually bite me and result in a lot of wasted time and effort.
Has anyone used XSLT for this sort of thing? What were your experiences? Would you recommend that I continue investigating it for use as a PHP template engine?