Home

Using Templates with PHP

|
Updated:  
2016-03-26 13:57:26
|
HTML5 and CSS3 All-in-One For Dummies
Explore Book
Buy On Amazon

As web development becomes standardized, the Model-View-Controller (MVC) architecture has become more popular. The basic idea of this mechanism is to separate the data elements (the model), the user interface (the view), and the code that connects the model and the view (the controller).

Many programming instructors emphasize separating content, layout, and data. However, the way PHP is often written, it combines all three elements. As a response, web developers often use some form of templating system to try to separate the content from the view.

The simplest form of a template is something like the following code:

<?php include_once("vars.php") ?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title><?=$title?></title>
</head>
<body>
  <h1><?=$heading?></h1>
  <div id = "main">
    <?=$content?>
  </div>
  <div id = "footer">
    <?=$footer?>
  </div>
</body>
</html>

The page holds the structure, but none of the actual contents. The contents are stored in PHP variables, which are stored in another file, called (in this example) vars.php. Here's what vars.php might look like:

<?php
$title = "template demo";
$heading = "Template Demo";
$content = <<<HERE
This is a very simple template example, which allows the user to load
content as variables from an external file. Of course, template
systems get much more complex than this.
HERE;
$footer = "from HTML5 / CSS All in One for Dummies";
?>

In this extremely simple example, the second PHP file simply defines variables containing the various values, achieving separation of data from view. Often, the secondary PHP file is more sophisticated, grabbing contents from a database or other storage medium.

Note that =$varname?> is a shortcut mechanism commonly used in templating situations. It opens PHP, prints the value associated with the variable name, and then quickly returns to HTML. When PHP is used for templating, you frequently pop in and out of PHP.

Of course, a number of template engines today add a great deal more to templating. One of the more prominent is Twig.

Twig takes a template written in a special simplified format and compiles it to PHP, allowing you to write sites like this (example from the Twig documentation):

<!DOCTYPE html>
<html>
    <head>
        <title>My Webpage</title>
    </head>
    <body>
        <ul id="navigation">
        {% for item in navigation %}
            <li><a href="{{ item.href }}">{{ item.caption }}</a></li>
        {% endfor %}
        </ul>
        <h1>My Webpage</h1>
        {{ a_variable }}
    </body>
</html>

Twig allows you to use a PHP-like syntax to create powerful templates. It also includes features for populating the variables that help to separate the data from the presentation and the control.

A number of other popular template engines include Smarty and Savant.

About This Article

This article is from the book: 

About the book author:

Andy Harris earned a degree in Special Education from Indiana University/Purdue University–Indianapolis (IUPUI). He taught young adults with severe disabilities for several years. He also taught himself enough computer programming to support his teaching habit with freelance programming.
Those were the exciting days when computers started to have hard drives, and some computers connected to each other with arcane protocols. He taught programming in those days because it was fun.
Eventually, Andy decided to teach computer science full time, and he still teaches at IUPUI. He lectures in the applied computing program and runs the streaming media lab. He also teaches classes in whatever programming language is in demand at the time. He has developed a large number of online video-based courses and international distance education projects.
Andy has written several books on various computing topics and languages including Java, C#, mobile computing, JavaScript, and PHP/MySQL.
Andy welcomes comments and suggestions about his books. He can be reached at [email protected].