CHAPTER 11 – TEMPLATE SYSTEMS

Template systems are PHP components that let you separate application logic from display logic, and offer a simpler template format than PHP itself. It is ironic that PHP, which essentially started out as a template lan- guage, is used to implement template systems. But, there are good reasons for doing this besides the code/presentation separation, such as giving web designers a simpler markup format they can use in their page authoring tools, and developers greater control over page generation. For example, a template system can automatically translate text snippets to another language, or fill in a form with default values. A vast number of template systems are available for PHP. This is caused by the fact that along with database abstraction layers, template systems are one of the PHP components that arouse the strongest feelings and little will for compromise in developers. As a result, many people have written their own template system, resulting in a wonderful diversity and lack of standardiza- tion.

Template Terminology Before you dive into the various template systems, you may want to familiar- ize yourself with the template lingo (see Table 11.1).

Table 11.1 Template Glossary Word Meaning Template The output blueprint; contains placeholders and blocks. Compile Transforming a template to PHP code. Placeholder Delimited string that is replaced during execution. Block or Part of a template that may be repeated with different data. Section

HTML_Template_IT The first PEAR template system you will familiarize yourself with is HTML_Template_IT, or just IT. This is the most popular PEAR template package, but it is also the slowest because it parses templates on every request and does not compile them into PHP code. Tip: The HTML_Template_Sigma package provides an API that is compatible with HTML_Template_IT, but compiles templates into PHP code.

Placeholder Syntax IT uses curly braces as placeholder delimiters, like this: 4 <head><title>{PageTitle}</title></head> This is the most common placeholder syntax, so chances are a template using only placeholders will actually work with different template packages.

Example: Basic IT Template This example is "Hello World" with HTML_Template_IT: <?php require_once "HTML/Template/IT.php"; $tpl = new HTML_Template_IT('./templates'); $tpl->loadTemplateFile('hello.tpl'); $tpl->setVariable('title', 'Hello, World!'); $tpl->setVariable('body', 'This is a test of HTML_Template_IT!'); $tpl->show(); First, you create an HTML_Template_IT object, passing the template direc- tory as a parameter. Next, the template file is loaded and some variables are set. The variable names correspond to placeholders in the template file, so the {title} template placeholder is replaced with the value of the "title" vari- able. Finally, the show() method does all the substitutions and displays the template output. This template file is used in this example: <html> <head> <title>{title}</title> </head> <body> <h1>{title}</h1> <p>{body}</p> </body> </html> Figure 11.1 shows the result. Fig. 11.1 Basic IT template output.

Block Syntax For blocks, IT uses HTML begin/end comments like this: <!-- BEGIN blockname --> <li>{listitem} <!-- END blockname --> Blocks may be nested, but it is important that you start processing at the innermost block and work your way out. Example: IT With Blocks First, install HTML_Template_IT: $ pear install HTML_Template_IT downloading HTML_Template_IT-1.1.tgz ... Starting to download HTML_Template_IT-1.1.tgz (18,563 bytes) ......done: 18,563 bytes install ok: HTML_Template_IT 1.1

This example uses blocks to implement a simple foreach-like loop in the template: <?php require_once "HTML/Template/IT.php"; $list_items = array( 'Computer Science', 'Nuclear Physics', 'Rocket Science', ); $tpl = new HTML_Template_IT('./templates'); $tpl->loadTemplateFile('it_list.tpl'); $tpl->setVariable('title', 'IT List Example'); foreach ($list_items as $item) { $tpl->setCurrentBlock("listentry"); $tpl->setVariable("entry_text", $item); $tpl->parseCurrentBlock("cell");

} $tpl->show(); This example sets up the IT object like the previous one, but calls setCur- rentBlock() that specifies to which block the following setVariable() call applies. When parseCurrentBlock() is called, the block is parsed, placeholders are substituted, and the result is buffered until the template is displayed. This is how the block template appears <html> <head> <title>{title}</title> </head> <body> <h1>{title}</h1> <ul> <!-- BEGIN listentry --> <li>{entry_text}</li> <!-- END listentry --> </ul> (End of list) </body> </html> Figure 11.2 shows the results.

Fig. 11.2 IT with blocks output. Finally, IT lets you include other template files anywhere in your tem- plate, like this: <!-- INCLUDE otherfile.tpl --> In this block example, you could substitute the block contents with just an include tag, and HTML_Template_IT would include that file for every iteration of the block. By using includes carefully, you can structure your templates so you obtain reusable sub-templates.

Post Comment
Login to post comments