Templating Systems

A templating system provides a way of separating the code in a web page from the layout of that page. In larger projects, templates can be used to allow designers to deal exclusively with designing web pages and programmers to deal (more or less) exclusively with programming. The basic idea of a templating system is that the web page itself contains special markers that are replaced with dynamic content. A web designer can create the HTML for a page and simply worry about the layout, using the appropriate markers for different kinds of dynamic content that are needed. The programmer, on the other hand, is responsible for creating the code that generates the dynamic content for the markers.

To make this more concrete, let’s look at a simple example. Consider the following web page, which asks the user to supply a name and, if a name is provided, thanks the user:

<html>
  <head>
    <title>User Information</title>
  </head>

  <body>
    <?php if (!empty($_GET['name'])) {
      // do something with the supplied values ?>

      <p><font face="helvetica,arial">Thank you for filling out the form,
      <?php echo $_GET['name'] ?>.</font></p>
    <?php }
    else { ?>
      <p><font face="helvetica,arial">Please enter the
      following information:</font></p>

      <form action="<?php echo $_SERVER['PHP_SELF'] ?>">
        <table>
          <tr>
            <td>Name:</td>
            <td>
              <input type="text" name="name" />
              <input type="submit" />
            </td>
          </tr>
        </table>
      </form>
    <?php } ?>
  </body>
</html>

The placement of the different PHP elements within ...

Get Programming PHP, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.