Introduction to Smarty Template Engine for Absolute Beginners

, , Leave a comment

This tutorial is mainly for the absolute beginners, it’s all about the basic introduction of the Smarty Template Engine.

What is Smarty?

Smarty is a template engine (or framework) for PHP. It provides a feasible way to separate application business logic and content data from the presentation. This would be best beneficial in a situation where the programmer and the template designer playing different roles.

Smarty 3.x versions requires a web server running PHP 5.2 or greater.

Smarty 2.x versions requires a web server running PHP 4 or 5.

Installation Process

  • Download Smarty Template Engine from
  • Extract the downloaded package on your server.

Smarty File Structure

The below section displays the default structure of the Smarty. 

Smarty Default Structure

Above file structure is the default one that comes with the fresh Smarty Package. In order to work with Smarty you need to create some more folders required by Smarty.

Smarty requires four directories to be created which are by default named as templates/, templates_c/, configs/ and cache/. These directories are defined by the Smarty class properties $template_dir, $compile_dir, $config_dir, and $cache_dir respectively.

Properties Description

$template_dir – Default template directory. By default this is ./templates. Smarty will check for the templates/ directory in the same directory as the executing php script.

$compile_dir  – Directory where compiled templates are located. By default this is ./ templates_c. Smarty will check for the templates_c/ directory in the same directory as the executing php script. Make sure that this directory is writeable by the web server.

$cache_dir    –  Directory where template caches are stored. By default this is ./cache. Smarty will check for the cache/ directory in the same directory as the executing php script. Make sure that this directory is writeable by the web server.

Making Simple Hello User Application

Now we are going to make simple Hello World application using Smarty Template Framework. Here we are making just demo application called “smarty_demo”.

Make one directory called ‘lib’ in our application root and copy paste the content of the fresh Smarty package’s “libs” folder into this newly created “lib” folder.

Let’s make one file called index.php in the root of application and make one file called index.tpl inside the templates directory. As I mentioned above four new directories are also required in Smarty. So make the following directories inside the root of our application.


Check the below image, you will have better idea about the final file structure.

Smarty uses a PHP constant named SMARTY_DIR which is the full system file path to the Smarty

libs/ directory. Basically, if your application can find the Smarty.class.php file, you do not need to set the SMARTY_DIR as Smarty will figure it out on its own. Therefore, if Smarty.class.php is not in your include_path, or you do not supply an absolute path to it in your application, then you must define SMARTY_DIR manually. SMARTY_DIR must include a trailing slash/.

Now open index.php and add the below code:

$smarty = new Smarty;
$smarty->caching = true;
$smarty->cache_lifetime = 120;
$smarty->assign("name", "Guest User");

Now open index.tpl and add the below code:

Hello {$name}, welcome to Smarty!

Point your browser to this smarty_demo application and it will print below message:

Hello Guest User, welcome to Smarty!

If you face an error saying the Smarty.class.php file could not be found, then visit the below link, it
might be helpful:

If you get the above message then enjoy.!! You have correctly installed the Smarty and done with simple Hello User application. We will see other possible aspects of the Smarty in detail in next tutorials.

Features of Smarty

  1. Its easy to learn.
  2. Its very fast.
  3. One time compilation, no template parsing overhead
  4. Really very Smart! (recompile only changed template files).
  5. Scalable for large applications and high traffic websites
  6. Provides built in caching support
  7. Allow configuration of template {delimiter} tag syntax. and many more…


This article is dedicated to absolute beginners. A lot more things can be done (or possible) with Smarty that I can’t write down all of them in single post. I hope this should give you very clear basic idea to start with it. So let’s give a try for the amazing “Smarty Templating Framework”.