Template Variables

Template files are the meeting place of static content (HTML) and dynamic values (variables), as well as minimal logic, similar to the examples shown in the previous section, to account for the numerous variations that are possible in a dynamic site. While there are many approaches and philosophies for templating in general, PHPTemplate has evolved in a way that encourages simple and minimal logic. It uses variables that are pushed into the template from the theme engine.

Each template will have a number of variables available to it that are provided by the theme engine. In addition to the variables that are unique to each template, the variables $id and $zebra are always included by PHPTemplate, no matter which template is being called. These variables work as follows:

• $id: A sequential counter that is incremented every time the template is rendered. In the case of blocks, for example, it allows each block to have a unique ID on the page.

• $zebra: An alternating counter that has the value odd or even. This is useful for giving table rows alternating colors, for example.

Furthermore, the variable $is_front is also available. It is true whenever the front page is being displayed, so you can always write template code like this:

<?php if ($is_front) : ?> -- display when on front page -- <?php endif;?>

0 0

Post a comment