CHAPTER 11 – HTML_Template_Flexy

The next template package is HTML_Template_Flexy, or just Flexy. Even though pure placeholder templates written for IT will work out-of-the-box with Flexy, these two template packages are very different. First, Flexy operates on objects and object member variables instead of variables that are in turn stored in associative arrays as with IT. This is not a big difference in itself, but Flexy has the advantage that you can give it any object, of any class, and your template can access its public member variables.

Example: Basic Flexy Template Here is a "Hello, World!" example with Flexy: <?php

require_once 'HTML/Template/Flexy.php'; $tpldir = 'templates'; $tpl = new HTML_Template_Flexy(array( 'templateDir' => 'templates', 'compileDir' => 'compiled', )); $tpl->compile('hello.tpl'); $view = new StdClass; $view->title = 'Hello, World!'; $view->body = 'This is a test of HTML_Template_Flexy'; $tpl->outputObject($view); A little more code is required to set up Flexy because you need to specify both the template directory and compile directory. The compile directory is where the compiled template files are stored. This directory must be writable by the web server. By default, the compile directory is relative to the template directory. Next, the hello.tpl template is compiled. You should notice that this is the same template as in the first IT example; this works because the template contains only two simple placeholders. Compilation is time-consuming, but is done only once or whenever the template file changes. As a result, you will notice that the first time you load this page, it takes a long time. Subsequent page loads are much faster. When a template is compiled, the compiled version is placed in compileDir. In the previous example, this is the "compiled" directory relative to the current directory. This directory must be writable by the web server, because templates will be compiled on demand by PHP when a user hits the page. Finally, an object holding view data is created and passed to the o u t p u t O b j e c t ( ) method, which executes the template and prints the output.

Example: Flexy with Blocks This example corresponds to the "IT with Blocks" example: <?php

require_once 'HTML/Template/Flexy.php'; $tpldir = 'templates'; $tpl = new HTML_Template_Flexy(array( 'templateDir' => 'templates', 'compileDir' => 'compiled', )); $tpl->compile('flexy_list.tpl'); $view = new StdClass; $view->title = 'Flexy Foreach Example'; $view->list_entries = array( 'Computer Science', 'Nuclear Physics', 'Rocket Science', ); $tpl->outputObject($view); This time, the template file is different because it is using more than just placeholders and is no longer compatible with IT: <html> <head> <title>{title}</title> </head> <body> <h1>{title}</h1> <ul> {foreach:list_entries,entry_text} <li>{entry_text} {end:} </ul> (End of list) </body> </html> If you compare the PHP code in this example with the corresponding IT example, you see that all the hassle of parsing blocks is gone. This is because the template is compiled; instead of dealing with flow-control on its own, Flexy leaves this to PHP's executor. Look at the PHP file generated by the Flexy compiler: <html> <head> <title><?php echo htmlspecialchars($t->title);?></title> </head> <body> <h1><?php echo htmlspecialchars($t->title);?></h1> <ul> <?php if (is_array($t->list_entries) || is_object($t >list_entries)) foreach($t->list_entries as $entry_text) {?> <li><?php echo htmlspecialchars($entry_text);?> <?php }?> </ul> (End of list) </body> </html>

Post Comment
Login to post comments