Welcome to PHP Template Inheritance

Important Notice: Developement has been frozen until further notice.

Unfortunately I found a bug which is not fixable within the current design.

The problem I noticed is that outside the first include()d template, the local variables of the including script are lost.

It's still possible to use $GLOBALS or some other mechanism (Eg. Singleton Pattern) to access variables in the base or intermediate templates, but the simple elegance I was shooting for just isn't there right now.

I have some ideas on how to fix this issue, but it'd probably at least clutter up the interface and might have different kinds of tradeoffs, so I'm not able to predict if I'll come up with a nice solution soon.

If you have a nice idea on how to implement PHP Template Inheritance in spirit, please drop me a mail at daniel@dornhardt.com.

It has been interesting to set this place up, so thanks everybody for coming bye - and thanks for all the fish. :)

PHP Template Inheritance is a free little templating hack for php 4 and php 5.

I like Django's template inheritance, and that's why I decided to create something similar for PHP. To the point:

Pros:

  • Straight PHP 4+, no dependencies
  • < 60 lines of code (without comments)
  • Pretty fast (under most circumstances)
  • Templates are PHP files, too, so you can do (almost) whatever you want there
  • Flexible: Templates can be extended inline too, special template files are not required

Cons:

  • Basically a hack and currently in beta-testing and
  • can add overhead when not used carefully / properly, please see the documentation for details.

Why I did this

Actually PHP should get this built into it's core ASAP IMHO. Then it could be implemented properly and not just as a hack.

I had some itches to scratch with this, and it's working pretty nicely for my taste. If somebody has a better idea on how to implement this, I'll gladly throw everything out and write this in a entirely different way. Or if somebody knows a nice and simple alternative which runs on all PHP4+ - Servers and still keeps PHP's flexibility available inside of the template files, I'd really like to hear about it.

Some approaches I didn't choose

A PHP Extension or Core Hack:

Because it wouldn't work on most webservers.

A "Template Parser"-Solution

A "Template Parser", which would read the templates, parse them for stuff, emulate loops, interpolate variables etc.

Why not?

a) it would probably suck, and
b) because you'd lose all the awesome flexibility PHP has to offer in it's strange ways of doing things. And actually because PHP ist almost the perfect templating language (if it only supported template inheritance).

The Future

Is as yet unknown, though I may update this from time to time, I hope I'll be able to incorporate some feedback into the code.

Some things on my Todo-List are:

  • Some error checking and error messages
  • More testing / Unit tests for common use cases

If you need support for integrating PHP Template Inheritance into your favourite framework or need help using it, please use the sourceforge.net system to file a ticket / bug report or ask for advice.