1.6. Templates

Templates make it easier for developers to maintain a consistent look and feel across many pages, they help keep your code organized, and they move presentation logic out of your code, making both your PHP and HTML files more readable. There are a lot of different templating products available — some big (like Smarty, http://smarty.php.net) and some small (TinyButStrong, www.tinybutstrong.com). Each have their own benefits and drawbacks regardless if the solution is commercial, open source, or home-brewed. Sometimes the choice of which one to use will boil down to a matter of personal preference.

Speaking of personal preference, although I love the spirit of templating, I'm not a fan of most implementations. Despite all the benefits, modern templating systems complicate things. Some have their own special syntax to learn and almost all incur additional processing overhead. Truth be told, most projects don't need a dedicated template engine; PHP can be considered a template engine itself and can handle templating for even moderately large web projects with multiple developers if proper planning and organization is in place.

The setup that works best for me is to keep the core of my presentation in specific HTML files in a templates folder. This folder is usually outside of the web-accessible base (though the CSS, JavaScript and image files referenced in the HTML do need to be publically accessible) since I don't want a visitor or search engine to stumble upon a slew of content-less pages.

For now, here's a basic template that's suitable for the needs of this project:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 <head>
  <title>
<?php
if (!empty($GLOBALS['TEMPLATE']['title']))
{
    echo $GLOBALS['TEMPLATE']['title'];
}
?>
</title>
  <link rel="stylesheet" type="text/css" href="css/styles.css"/>
<?php
if (!empty($GLOBALS['TEMPLATE']['extra_head']))
{
    echo $GLOBALS['TEMPLATE']['extra_head'];
}
?>
 </head>
 <body>
  <div id="header">

<?php
if (!empty($GLOBALS['TEMPLATE']['title']))
{
    echo $GLOBALS['TEMPLATE']['title'];
}
?>

  </div>
  <div id="content">
<?php
if (!empty($GLOBALS['TEMPLATE']['content']))
{
    echo $GLOBALS['TEMPLATE']['content'];
}
?>

  </div>
  <div id="footer">Copyright &copy;<?php echo date('Y'), ?></div>
  </div>
 </body>
</html>

There should be some conventions in place to keep things sane. For starters, content will be stored in the $GLOBALS array in the requested script so it will be available in any scope within the included template file. I commonly use the following keys:

  • title — Page title

  • description — Page description

  • keywords — Page keywords (the page's title, description and keywords can all be stored in a database)

  • extra_head — A means to add additional HTML headers or JavaScript code to a page

  • content — The page's main content

Occasionally I'll also include a menu or sidebar key depending on the project and planned layout, though your exact variables will depend on the template. So long as there are standard conventions written down and faithfully adhered to, a development team of any size can work successfully with such a template solution.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.110.0