WordPress is a pretty great piece of software, and the ecosystem around it is equally brilliant, but it’s probably fair to say that many a plugin is hacky and horrible inside. Everything modern programmers have learnt about separation of concerns, indentation, readability and object orientation seems to be lobbed out of the window where WordPress is involved – perhaps an unfortunate side-effect of its (laudable) low barriers to entry.
However, it doesn’t have to be this way! I wrote a simple set of classes which allows WordPress plugins to be well-structured, separates view layers out from business logic, and offers partials (HTML fragments) and components (logic plus fragment). Although this was written for WordPress, it can in fact be used for any PHP project where a simple templating system is required.
True to form, soon after writing this, I discovered that many other WP programmers had tackled the same problem – I should have checked! Theirs will, in several cases, be more feature-filled and better-tested, but nevertheless I offer mine in the ‘lightweight’ category. I’ve used it to good effect, and perhaps others will similarly find it useful. Released under the same license terms as WordPress itself.
Updates
15 June 2013: The architecture of this could be improved slightly, I’ll see if I can get around to fixing (patches welcome, of course). At present, the controller runs a pre-execute method inside the constructor, which isn’t ideal for unit testing.
7 July 2013: I’ve set up some better entry points to the controller (pre, execute, post) and, given that this new version is incompatible with the previous release, I’ve introduced namespacing, so incompatible versions can coexist. That means that, as of this change, the library requires PHP 5.3, which isn’t too onerous (even if many users haven’t yet upgraded from 5.2!).