PHP Templates

Contents

Introduction

PHP templates are files that contain mainly HTML code. However, in addition to the HTML code, you can specify certain variables that will be overwritten with the values of PHP variables so that your page can change dynamically. There are different engines that parse PHP templates into strictly HTML code, one of which is Smarty. Smarty is an object-oriented PHP library that does the parsing of the PHP templates.

Advantages of PHP templates

  • Easier to make changes
  • Code is cleaner

Disadvantages of PHP templates

  • All of the parsing may slow down the loading of pages

Installing Smarty

For simplicity, throughout the installation and usage sections, the root directory of you application will be called 'public_html'. To install Smarty, do the following:

  • Download the latest version of the library from here and extract it into the public_html folder.
  • Rename the extracted directory to 'smarty'.
  • Create a new directory in the 'smarty' directory called 'docs'
  • Create the following directories within the 'docs' directory:
    • cache
    • config
    • templates
    • templates_c
  • Make the 'templates_c' and 'cache' directories writable

Now you have successfully installed Smarty into the 'public_html/smarty' directory.

Using Smarty

All of your template files (*.tpl) should go into the 'public_html/smarty/templates/' directory. For a simple example, create a template file called 'simple.tpl' that contains the following:

<html>
    <head>
        <title>{$title}</title>
    </head>
    <body>
        {$message}
    </body>
</html>

Now create a new directory in the 'public_html' directory called 'smarty_example1'. In this directory create a file called 'index.php' that contains the following:

<?
    //set up Smarty
     
    require_once('../smarty/libs/Smarty.class.php');
    $smarty = new Smarty();
    $smarty->template_dir = '../smarty/docs/templates/';
    $smarty->compile_dir = '../smarty/docs/templates_c';
    $smarty->cache_dir = '../smarty/docs/cache';
    $smarty->config_dir = '../smarty/docs/configs';
    
    //display the page
    
    $smarty->assign('title', 'Smarty Example 1');
    $smarty->assign('message', 'This is a sample message');
    $smarty->display('simple.tpl');
?> 

The above code will work assuming that you have maintained the directory structure outlined above. If you have changed it, you will have to modify the require_once statement as well as the four directory locations under it in order to have them match up.

Now you can run your page, and you will notice that the title will be 'Smarty Example 1' and it will display 'This is a sample message'. This is because in the 'simple.tpl' file, we embedded variables called 'title' and 'message'. In the .tpl file, these variables must be enclosed within curly braces and start with a dollar sign, as follows: {$variable_name}.

Notice how, in our PHP code, after setting up the Smarty object, we assign to our 'title' and 'message' variables in our template the values we would like to be displayed, and Smarty does the rest.

A more involved example

What is outlined above is just the very basics of what Smarty is capable of. It also has many very useful features, such as a looping feature to display variables that are arrays. To take a look at a more involved example, download Media:smarty_example2.tar and extract the directory into your 'public_html' directory, and download Media:table_templates.tar and extract the files into your 'public_html/smarty/docs/templates' directory. You will then be able to run it.

The main purpose of this example is that you can display an entire HTML table by defining a two arrays: one array defines the table header, and the other array, a two-dimensional array, defines the cells. You can experiment by giving it a variable number of rows and columns and it will still display properly. You can see a demo of it here.

Related Links