PHP as a template engine

by Theditor on Jun.30, 2010, under General News

Believe it or not, PHP is a template engine. It allows you to mix HTML and PHP into a file and makes it very easy for a programmer to create dynamic pages. However, one excuse that I’ve heard from would-be programmers shunning away from PHP is that they don’t like to mix programming code with HTML code in 1 file. They prefer to keep code in 1 file and HTML in separate file.

Have no fear, here is my simple guide on how to build your own basic template system using PHP.

So let’s begin with this tutorial.

The main function

We first define 1 array. We’ll call it $tpl_data. This allows us to pass variables from our functions to the template rendering function.

$tpl_data = array()

Now we will create the the function that will call the template rendering function.

    function main(){
    global $tpl_data;

    $tpldata['hello'] = “Hello World”;

    echo get_template(‘index.tpl.php’);

    }

The first line in the function is

global $tpl_data;

This line allows the function to populate this variable. By doing this, you can have multiple functions pass variables to the template rendering function before actually rendering the template.

$tpldata['hello'] = “Hello World”;

This is where you populate the $tpl_data array. In this case, I’m passing the phrase “Hello World” as the variable $hello to the template rendering function.

echo get_template(‘index.tpl.php’);

We will then call the template rendering function. We will instruct the function to render the file index.tpl.php and output the result to screen.

Template Rendering Function

Now let’s look at the template rendering function.

    function get_template($filename) {
    global $tpl_data;

    if ($tpl_data) extract($tpldata,EXTR_SKIP);

    ob_start();
    include “templates/$filename”;
    $contents = ob_get_contents();
    ob_end_clean();
    return $contents;
    }

Once more, we begin the function with the following line.

global $tpl_data;

This is to ensure that your variables are available in the template rendering function. You control what variables can be used within the template via the $tpl_data array. This adds a layer of security, crude and basic but it’s simple and it works well.

if ($tpl_data) extract($tpldata,EXTR_SKIP);

This line extracts tpl_data array values into their own variables. For example,

tpl_data['key'] = “value”

will become

$key = “value”

Now we’ll get into the nitty-gritty template rendering stuff.

    ob_start();
    include “templates/$filename”;
    $contents = ob_get_contents();
    ob_end_clean();

We turn output buffering on by calling the “ob_start” function. This is a built-in function in PHP. While output buffering is on, no output will be sent to screen. Instead, it will be stored in a temporary output buffer.

    include “templates/$filename”;
    $contents = op_get_contents();

We then include the template file. In this case, the file is stored in the folder called “templates” in the same folder as the script. After that, we assign the data in the output buffer into the variable $contents.

    ob_end_clean();
    return $contents;

This ob_end_clean function is then executed to clean out the output buffer the value of $contents is returned to the function calling the template rendering function.

The template

Now let’s create the template. It’s going to be a simple HTML file with some PHP code in it.

    <html>
    <body>
    <?php echo $hello; ?>
    </body>
    </html>

Basic PHP 101. Echo the value of $hello. Notice that the variable name is $hello, not $tpl_data['hello']. This is because of the “extract” statement in the template rendering function. Once completed, save this file into the

    templates/

folder under the name index.tpl.php.

Now that your template is done, return to your php script. We will now call the main() function to get the ball rolling. In the last line before you close your php file up, enter:

    main();

Save and run your script. You will get this

    Hello World

The template is still a full-fledge php file. You can use any PHP code within the template.

And there you have it, a basic template engine written in PHP. You can further enhance the template rendering function with gzip functions, cache functions and security functions. I will leave that to your imagination and skill :)


Comments are closed.

Our Recent Clients

A list of our recent clients

Random Tips

Do you have a tip you would like to share? Send it in and we'll add it to the site. Don't forget to include your name so that we can give kudos to you