Hudzilla.org - the homepage of Paul Hudson
Contents > Miscellaneous topics > Templates Wish List | Report Bug | About Me ]

16.8.4     Writing your own

This is NOT the latest copy of this book; click here for the latest version.

Templates are not something I generally recommend because there are a number of potential pitfalls that are all-to-easily fallen into. However, in order to make your life easier, I've put together a short list of the hints and tips I've learnt the hard way over the years so that you can learn from them:

  1. Start small, and work your way up. If you try to put together a list of all the different things you want to do at the beginning, you're likely to make the system more complex. Instead, think of the most important four or five template objects, then do the rest as you encounter a need for them.

  2. Using str_replace() for templates can be optimised by passing in the parameters as arrays.

  3. Use a code caching system or you'll regret it - templates are notoriously slow devices, and it's cheaper to get a decent code cache than it is to just buy more servers.

  4. Don't be afraid to split up your system into multiple smaller files for easier manageability; template systems have a tendency to get more complex with each code update, and anything that helps is a good thing.

  5. The best template systems I've seen have been object-oriented - it's an entirely personal decision how you code yours, but do consider OO first.

  6. If you can statically cache pages, do so. This makes for a big performance boost.

  7. Although it's possible to run complex regular expressions for your template interpretation, it causes a real speed hit: evaluate each one carefully.





<< 16.8.3 When not to use templates   16.9 Summary >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is zero plus seven?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow