HomePHP Template-Based Web Development With patTemplate (part 1)
Template-Based Web Development With patTemplate (part 1)
Most PHP-based Web sites are a mush of intermingled HTMLmarkupand PHP function calls, making them hard to decipher and maintain. Butthere *is* a simpler way - using templates to separate layout frombusinesslogic. This article shows you how.
One of the nice things about PHP - and one of the primary reasons for its popularity as a rapid application development (RAD) tool - is the fact that PHP code can be inserted into regular HTML markup to turn otherwise static HTML content into dynamic, intelligent Web pages. This feature makes it possible to quickly write PHP scripts that build Web pages on the fly from a database (or other external data source), and to create "smart" Web applications more efficiently than would otherwise be possible with traditional programming languages like Java or Perl.
However, this ease of use comes with a price: most PHP-based Web sites are a mush of intermingled HTML markup and PHP function calls, making them hard to decipher and maintain. This problem most commonly rears its ugly head when interface designers need to alter the user interface presented to Web site visitors - since the presentation information is entwined with PHP code, changes to it typically require handholding by a developer with sufficient expertise in the language. Which ultimately means more people, more time and more money...
There is, however, an alternative.
This alternative solution involves using "templates" to separate presentation and layout information from program code, and a template engine to combine the two to create the final product. This two-tiered approach affords both developers and designers a fair degree of independence when it comes to maintaining a Web site, and can substantially reduce the time and effort required in the post-release phases of a development project.
Despite these advantages, this template-based approach is not that popular - or even that well-known - amongst developers, especially those that are new to Web development. And so, over the course of this two-part article, I will be attempting to demystify how it works, in the hope that it will encourage you to use it in your next development effort.