O'Reilly logo

Learning PHP and MySQL by Jon A. Phillips, Michele E. Davis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Templates

Templates separate the HTML code that defines the presentation or look of a page from the PHP code that's responsible for gathering the data. Once separated, it becomes easier for someone with HTML and perhaps CSS knowledge to modify the template without worrying about breaking the PHP code. Likewise, the PHP code can focus on the data instead of getting caught up in presentation details.

There are other advantages to using templates, too. If you make a mistake in the template, the error will be clearly returned from the template. The template itself can generally be loaded into a web browser or a graphical web development tool such as Dreamweaver, since it resembles the final state of the page when processed. Templates support very basic programming features for use with presentation, such as being able to tell whether a section of a page should be visible.

Of course, nothing's perfect; there are a couple of disadvantages to templates. Templates increase the number of files to maintain. They add a small amount of extra processing time. They also require installing the template engine and setting up directories. You need to be running at least PHP Version 4.0.6 to use Smarty, a popular template engine.

Template Engine

There are several template packages available on the Internet. Each uses its own template engine to process the templates and make them as efficient as possible. No matter which template engine you use, you'll always follow the same basic steps:

  1. Retrieve your ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required