Приглашаем посетить
Чулков (chulkov.lit-info.ru)

Writing Your Own Template Solution

Previous
Table of Contents
Next

Writing Your Own Template Solution

If your development and design teams have the self-discipline to separate display and application logic without any language-level enforcement of the separation, then using plain PHP as an ad hoc template system is a good solution. PHP began as a template language, designed to glue various C functions together to make HTML pages. Although PHP has evolved from a simple glue language into a versatile general-purpose scripting language, it has remained true to its roots and still excels at templating.

The basic strategy is to write templates that are like compiled Smarty templates. Here is a basic class to handle rendering templates:

class Template {
  public $template_dir;
  function display($file) {
    $template = $this;
    // suppress non-existent variable warnings
    error_reporting(E_ALL ~ E_NOTICE);
    include("$this->template_dir.$file");
  }
}

To use this template class, you create a new Template object, populate it with the data you want, and call display(). The Template object itself will be visible as $template. The hello template for this class looks like this:

<html>
<title><?php print $template->title ?></title>
<body>
Hello <?php print $template->name ?>!
</body>
</html>

The PHP to call the template is as follows:

$template = new Template;
$template->template_dir = '/data/www/www.example.org/templates/';
$template->title = 'Hello World';
$template->name = array_key_exists('name', $_GET)?$_GET['name']:'Stranger';
$template->display('default.tmpl');

As with Smarty, with PHP you can encapsulate default data in the class constructor, as shown here:

class Template_ExampleOrg extends Template
{
  public function _ _construct()
  {
    $this->template_dir = '/data/www/www.example.org/templates/';
    $this->title = 'www.example.org';
  }
}

Because templates are executed with the PHP function include(), they can contain arbitrary PHP code. This allows you to implement all your display logic in PHP. For example, to make a header file that imports CSS style sheets from an array, your code would look like this:

<!-- header.tpl -->
<html>
<head><title><?php print $template->title ?></title>
<?php foreach ($template->css as $link) { ?>
  <link rel="stylesheet" type="text/css" href="<?php echo $link ?>"" />
<?php } ?>
</head>

This is an entirely appropriate use of PHP in a template because it is clearly display logic and not application logic. Including logic in templates is not a bad thing. Indeed, any nontrivial display choice requires logic. The key is to keep display logic in templates and keep application logic outside templates.

When you use the same language to implement both display and application logic, you must take extra care to maintain this separation. I think that if you cannot rigidly enforce this standard by policy, you have a seriously flawed development environment. Any language can be misused; it is better to have users willingly comply with your standards than to try to force them to.


Previous
Table of Contents
Next