10.2. TemplatesTemplates 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. 10.2.1. Template EngineThere 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:
We'll walk through this process with some examples shortly. One of the more popular template engines available is Smarty, shown in Figure 10-16. Smarty has many, many features, but what we're concerned most with is the basic template engine functionality. Figure 10-16. Smarty template engine10.2.2. InstallationWhile installing Smarty isn't as complex as installing and configuring Apache, PHP, and MySQL, it still deserves some attention.
10.2.2.1. Application level directoriesFor each application with which you wish to use Smarty, you'll need to set up a set of four directories. The four directories are for templates, compiled templates, cached templates, and configuration files. Although you may not use all of those features, you should set up the directories just in case.
All you need to do is create a template and a PHP file to try it out. 10.2.2.2. Creating sample scriptsNow set up your application in the document root. See Example 10-11. Example 10-11. The index.php file to create
The bulk of what's happening in Example 10-11 is telling your PHP program where to find the Smarty class file to include and the location of the application directories. Next, create myapp/index.php: <?php require_once("smarty.php"); $smarty->assign('test', '123'); $smarty->display('index.tpl'); ?> 10.2.2.3. Create a sample templateEdit the index.tpl file in your myapp/smarty/templates directory, as shown in Example 10-12. Example 10-12. The sample index.tpl template to create
Now, go to your new application through the web browser (http://www.domain.com/myapp/index.php, in our example). You should see something like Figure 10-17. Figure 10-17. Web browser-displayed codeNow you can convert the previous example to the version shown in Example 10-13. Example 10-13. Using the template to display the table
The template is a bit more complex, since you're dealing with rows coming back in arrays and there are multiple rows. The index2.tpl file is shown in Example 10-14. Example 10-14. The new table template
Example 10-14 outputs the screen shown in Figure 10-18. Figure 10-18. The output doesn't change even though you're using the templateThe template incorporates the looping element of Smarty. We used an associative array for returning your results to make the template easier to read, as the field names are the column names and not numbers. Smarty could have easily added some nice decorations, such as alternating the color of the row backgrounds. In the next chapter, we'll discuss more complicated database functions now that you have a good solid understanding of database functions. |