PHP

XTemplate updates

For those interested in PHP and Templating, I’ve released some updates to the (excellent – no bias!) PHP XTemplate, one of the major new features is that of Tag Callback modifiiers – this is a bit of a change of direction for the code, however it provides some neat potential and will certainly change the way I use the XTemplate code (that I’ve had a hand in writing and maintained for the last few years).

So tags look like {mytag} or {myarray.mytag} and these get replaced in your HTML by the values you’ve assigned in your code, now with a slight modification {mytag|mycallback} you can (at the point of the replacement) pass the data to your function before it’s output. So eh? what does that actually mean? Ok say you want to output some data and have it made into upper case prior to now, you’d have to put that logic into the PHP and keep your HTML template clear of any processing – e.g. a simple search and replace happened, the output had to all be prepared in the PHP. Now however, with the Tag Callback modifiers, you prepare the data, assign it for output, and at the point of output, you call some other (PHP) code and that modifies your output where it’s required.

Admittedly, uppercasing something is not that great, however take a larger system, say something with an international flavour that has to represent currency in different formats – you might have different folders with your templates in for each language, and prior to this feature being added to XTemplate, you’d have to apply logic in the PHP to decide on the output format, each time you wanted to output a number (OK that would probably be in a library function call but you’d still call that prior to output). Instead now, you can put that logic into the template thus: {mytotal|number_format(%s,2,’.’,’,’)} for UK Sterling, and for Euro {mytotal|number_format(%s,3,’,’,’ ‘)} – this of course can be simplified further {mytotal|sterling} and {mytotal|euro} if you’d defined those functions in your PHP.

I have found this quite a liberation as display related things are now (mostly) dealt with in the template – and it provides a new freedom for the designer to have more control over the (logic of) output. Consequently your PHP looks (even) cleaner than it did before and your templates are somehow more self contained.