Using PHP as a Template Engine

There are lots of template engines scattered across the web. Where I work we even wrote our own, which turned out to be a lot like Tiny But Strong. But, if you're using PHP, you probably don't need a separate template engine; PHP is already a template language.

One of the main reasons for a template engine is to separate display and the source code logic. Below I'll describe how I do that using just PHP. There is nothing special here, this is just my coding standard for using PHP as a template engine. Particularly in a Model View Controller (MVC) paradigm.

The Controller

For the controller, I simply create a .php file. This file should include only PHP code and should not output any HTML. It should just setup variables and/or arrays that can be used by the view. The controller does all the heavy lifting then includes the view to display.

Here's a very simple example of a controller.

<?php

  // Setup the page title
  $title = 'Hello World';

  // Include the view
  require 'example.html';

?>

The View

For the view, I simply create an .html file. This file will contain mostly HTML but PHP short tags can be used to fill in as merge codes.

Here's an example of a simple page.

<html>
  <head>
    <title><? echo($title); ?></title>
  </head>
  <body>
    <h1><? echo($title); ?></h1>
    <? echo($title); ?>
  </body>
</html>

You can also avoid the echo() function if you use the following short tags.

<?= $title ?>

Here are a few points I try stick to.

  1. I force myself to use functions in the HTML, keeping the code simple. Built in PHP functions are okay, as long as I only use one, such as echo or include.
  2. All the heavy lifting is done through the controller.

At times, you may want to put my source code logic at the bottom of the main file. Technically, then, the logic and source are separated top and bottom but not in separate files. There are lots of cases when this is desirable, such as when you prefer to distribute a single php file for something simple.

Short Tags vs Long Tags

I generally use long PHP tags (<?php … ?>) when I'm coding. This is considered good practice because almost all hosts support them. But, when I'm using PHP as a templating engine, I find it more readable to use short tags (<? … ?>). This is generally acknowledged as a bad idea, because there is less server support, but I believe it improves readability slightly. Honestly, I haven't used a host that didn't support short tags inside of .php files, so it's never been a problem for me. This rule only applies to code inside of HTML files and not to code inside of PHP files, where I use the full tags.

PHP Code in the View

In order to keep the View and Business Logic separate only a few types of PHP code are allowed in the HTML (The View).

  • Single functions
  • Alternate format If / Else / ElseIf blocks
  • Alternate format For loops
  • Alternate format Foreach loops
  • Alternate format Switch statements

PHP offers an alternative syntax for some of its control structures; namely, if, while, for, foreach, and switch. In each case, the basic form of the alternate syntax is to change the opening brace to a colon (:) and the closing brace to endif;, endwhile;, endfor;, endforeach;, or endswitch;, respectively. These alternate formats are the only formats that should be used in the view.

I will concede that the alternate PHP syntax is slower (because the interpreter is jumping in and out of PHP tags). But, this generally equates to milliseconds of processing time and makes little difference with today's servers in most environments.

Foreach Example

<? foreach($users as $user): ?>
    <p>
        First: <? echo($user['name']); ?><br>
        Last: <? echo($user['last']); ?>
    </p>
<? endforeach; ?>

If / ElseIf / Else Examples

<? if ($a == 5): ?>
    <p>A is equal to 5.</p>
<? endif; ?>
<? if ($a == 5): ?>
    <p>A is equal to 5</p>
<? elseif ($a == 6): ?>
    <p>A is equal to 6.</p>
<? endif; ?>

For Example

<? for ($i; $i <= 5; $i++) : ?>
    <tr>
        <td>0123456789</td>
        <td>Computer Programmer</td>
        <td>1 Day</td>
        <td>$550,000</td>
    </tr>
<? endfor; ?>

Gotcha

I've found one gotcha with this setup. If you separate your header and footer in separate files and the header file starts an if block (<? if (1=1): ?>) and the footer ends it, you'll get an error about an unexpected end. The solution is simply to place your if statement in both the header and the footer and properly end that statement in both places. Or, maybe you need to better separate your business logic from your view.

comments powered by Disqus
php/using_php_as_a_template_engine.txt · Last modified: 2020/06/01 22:53 (external edit)