Using Template Files

In Exercise 5-1, you saw how a function could be written to override a themable function. As useful as this is, generating HTML from within functions can be very tiresome work. PHPTemplate makes the process of creating themable functions much easier and modular by introducing template files. The required file page.tpl.php is one such template file that will be used whenever theme('page') is called. This template must be provided by the theme itself.

Other template files are provided by the PHPTemplate engine. These include block.tpl.php, box.tpl.php, comment.tpl.php, and node.tpl.php (see Figure 5-5 earlier in the chapter). These files override the block, box, comment, and node themable functions, respectively. If any of these four files appear in your theme folder alongside page.tpl.php, they will override the version in themes/engines/phptemplate.

The template files are completely reusable; you could trade and mix the five standard tpl.php files between themes and expect, at a minimum, that they will be called at the appropriate time. Furthermore, since they are include files and not functions, you can write all the

HTML code without needing to worry about escaping quotation marks or concatenating stringsā€”a fact that greatly improves the readability of template files over PHP functions that do the same thing.

0 0

Post a comment