Monday, February 13, 2006

using php and smarty templates

By Justin Silverton

Why use smarty templates?

One of Smartys primary design goals is to facilitate the separation of application code from presentation. Typically, the application code contains the business logic of your application, written and maintained in PHP code. This code is maintained by programmers. The presentation is the way your content is presented to the end user, which is written and maintained in template files. The templates are maintained by template designers.

At its most basic function, the application code collects content, assigns it to the template engine and displays it. The content might be something like the headline, tagline, author and body of a newspaper article. The application code has no concern how this content will be presented in the template. The template designer is responsible for the presentation. They edit the template files, adding markup and bringing it to completion. This typically involves things like HTML tags, cascading style sheets and other tools provided by the template engine.

This paradigm serves several purposes:

*) Designers can't break application code. They can mess with the templates all they want, but the code stays intact. The code will be tighter, more secure and easier to maintain.

*) Errors in the templates are confined to the Smartys error handling routines, making them as simple and intuitive as possible for the designer.

*) With presentation on its own layer, designers can modify or completely redesign it from scratch, all without intervention from the programmer.

*) Programmers aren't messing with templates. They can go about maintaining the application code, changing the way content is acquired, making new business rules, etc. without disturbing the presentation layer.

*) Templates are a close representation of what the final output will be, which is an intuitive approach. Designers don't care how the content got to the template. If you have extraneous data in the template such as an SQL statement, this opens the risk of breaking application code by accidental deletion or alteration by the designer.

*) You are not opening your server to the execution of arbitrary PHP code. Smarty has many security features built in so designers won't breach security, whether intentional or accidental. They can only do what they are confined to in the templates.

Although application code is separated from presentation, this does not necessarily mean that logic is separated. The application code obviously has logic, but the templates may have logic based on the condition that it is for presentation only. For example, if the designer wants to alternate table row colors or upper-case some assigned content, they can. This is presentation logic, something the programmer should not be concerned with. How often have you had some presentation displayed in a single column and then you wanted it in two or three columns, so the application code needs adjusting to accomodate this? A better approach is to assign the content in one single array and let the template handle the presentation. This will simplify your application and keep your templates flexible. Smarty supplies the tools to handle this kind of situation.

Getting Started with smarty

The first step is to download the smarty template engine Here

1. copy the following to a new file named "smartywrapper.php

// require the Smarty class

// extend the Smarty class
class smartywrapper extends Smarty {

//this will only work with php 5.X
function function __construct() {

// create the Smarty object

// make sure these folders exist and the permissions are set accordingly
$this->template_dir = '/www/example.com/webapp/template/';
$this->compile_dir = '/www/example.com/webapp/compile/';
$this->config_dir = '/www/example.com/webapp/config/';
$this->cache_dir = '/www/example.com/webapp/cache/';

2. copy the following to a file named "smartytest.php"

// require the new wrapper class we just created from above

// create the Smarty_WebApp object
$smarty = new smartywrapper();

// assign a variable, first parameter is the var name, second is the value
$smarty->assign('test_var_1','this is a test');

//display the template file (all html can now be placed here instead of in your php files)

create a file called my_template.tpl and place it in the directory in $this->template_dir = '/www/example.com/webapp/template/'; from above:

This is a test page from smarty, my variable is: {$test_var_1}

launch smartytest.php from you webbrowser and if it is successful, you should see the following


This is a test page from smarty, my variable is: this is a test


Smarty templates can not only be used to increase the overall speed of your php scripts through caching (more about caching can be found Here), but make it easier to develop large-scale applications through the separation of HTML code and php script.


Post a Comment

Links to this post:

Create a Link

<< Home