Using PHP as Your Template Engine (PTE)
written by: Tim Gallagher
11/12/2006
I've stopped using this exact technique in favor of frameworks that allow me to do essentially the same thing. For example, the CodeIgniter framework allows me to use PHP files for templates.
This article is an attempt to disseminate what I believe to be a "best practice" of PHP software development.
I read an article some time back about this very subject. I didn't pay it much attention, but as I started writing my own template engine and tried to work through some of technical issues of loops, and includes and other things a decent template system needs, I realized this work had already been done for me.
Separating the logic necessary to display information from the logic necessary to store, validate and process information is always a grand goal. It is a goal that I believe in, and a goal that PTE embraces.
Allow me to explain my decision to never again use a template in the sense of variable replacement and substitution. As I was writing a template engine, I came to the point of doing loops. I figured out a way to do this. Then came included files. Again, I found a way to do this. Then came inserted files (files that are inserted at the same level in the template as the insert tag) - figured this one out too.
Then I realized that I needed to do loops inside loops, loops inside included files, and worried about ending up needing a bunch more things, I realized I was well on my way to creating a pseudo language for my templates. About this time I ran across an article mentioning using PHP as a template language. I didn't pay it much attention, just kept using my template engine as best I could.
I never did implement code to handle loops within loops, but I did take the time to write a "compiler" for the engine. Can you guess what the compiler created?
That's right, PHP code! Still, it took me several rounds of web site modifications and site work to realize the importance of this discovery. Then I decided I would write a content management system (CMS). I decided to test the new idea of using PHP templates. Guess what, it worked - it worked great, and I've never had a more powerful templating system.
The upside of PTE...
- The full power of PHP is available
- No compiling
- Error reporting is handled by PHP
- No string parsing overhead
- No regular expression overhead (this can be pretty expensive)
- When using short tags, it actually looks like a template language.
- PHP is easy to learn for simple procedural things like loops and variable output.
- The full power of PHP is available (you might want to restrict access to the language)
- Output is immediate (this can be overcome with output buffering.)
- Templates need to be evaled if they reside in a database - making debugging difficult
- Template writers may need to be educated and learn some simple PHP
- There will be a temptation to put more than display logic in the templates
Continue Reading on Page 2
Jump to page: _1_ | _2_ | _3_ | _4_ | _5_ | _6_
Thank you for reading! Feedback may be left by using the contact form
Interesting and/or informative comments will be posted on the article comment page.
For web site hosting I recommend - www.bluehost.com

Others have spoken of Tim's technical expertise in PHP and MySQL, but the main thing that sets him apart from any other programmer I've worked with is he tries to get a deep understanding of my overall needs and business requirements.