1.6. Templates

Templates make it easier for developers to maintain a consistent look and feel across many pages, they help keep your code organized, and they move presentation logic out of your code, making both your PHP and HTML files more readable. There are a lot of different templating products available — some big (like Smarty, http://smarty.php.net) and some small (TinyButStrong, www.tinybutstrong.com). Each have their own benefits and drawbacks regardless if the solution is commercial, open source, or home-brewed. Sometimes the choice of which one to use will boil down to a matter of personal preference.

Speaking of personal preference, although I love the spirit of templating, I'm not a fan of most implementations. Despite all the benefits, modern templating systems complicate things. Some have their own special syntax to learn and almost all incur additional processing overhead. Truth be told, most projects don't need a dedicated template engine; PHP can be considered a template engine itself and can handle templating for even moderately large web projects with multiple developers if proper planning and organization is in place.

The setup that works best for me is to keep the core of my presentation in specific HTML files in a templates folder. This folder is usually outside of the web-accessible base (though the CSS, JavaScript and image files referenced in the HTML do need to be publically accessible) since I don't want a visitor or search engine to stumble upon a slew ...

Get PHP and MySQL®: Create-Modify-Reuse now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.