CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

PHP – Setting up “Smarty Template Engine”

Smarty is the popular web template engine written in PHP for PHP. Main goal of Smarty is clean and easy separation of presentation layer from business logic layer. With Smarty, it will be easy to write code for developing simple websites as well as complex structured websites.

Smarty achieved this by separating presentation layer to Smarty templates and keeping the business logic into PHP files. Smarty templates are written in simple HTML, with embedded Smarty tags which are enclosed with braces “{}”.

Smarty Template Engine is not delivered by default with PHP installation. You need to install it separately and configure, in order to use it. Hence this article.

Setting up “Smarty Template Engine”

Step 1. Download Smarty from Smarty’s website.

Step 2. Upload the file into your website and extract the uploaded file. Once you extracted, the folder structure looks like below:

Smarty-3.1.16
 - libs
   + plugins
   + sysplugins
   debug.tpl
   Smarty.class.php
   SmartyBC.class.php

All these files are required to run Smarty application. Remember that you should not edit the PHP files distributed with Smarty. Otherwise, Smarty may not work properly.

Step 3. In order to use Smarty, you must include Smarty.class.php file into your application. And you should set up a PHP constant SMARTY_DIR, pointing to the full system path of Smarty’s “libs” directory. It could be like this: assuming you extracted the Smarty into “/public_html/billing/includes/smarty” folder.

define('SMARTY_DIR', '/public_html/billing/includes/smarty/');
require_once(SMARTY_DIR . 'Smarty.class.php');

Remember that don’t forget to add trailing slash (“/”) to SMARTY_DIR.

Step 4. Smarty requires four directories which are:

  • templates – Used to store Template Programming Language (TPL) files.
  • templates_c – Smarty will generate PHP files after compiling TPL files. Smarty will use this folder to store compiled TPL files. This directory must be writable by Smarty.
  • configs – This is the directory used to store config files used in the templates.
  • cache – Where Smarty stores templates caches. This directory must be writable by Smarty.

Create these directories in the same folder where Smarty.class.php file exists. It is highly recommended that create these directories for each application that will use Smarty.

Step 5. If you want to create these four folders other than the specified location; you must provide the folder paths into Smarty class properties:

  • $template_dir – Provide full path to “templates” directory.
  • $compile_dir – Provide full path to “templates_c” directory.
  • $config_dir Provide full path to “configs” directory.
  • $cache_dir – Provide full path to “cache” directory.

These Smarty class properties and accessible through Smarty class’s object.

$smarty = new Smarty();

Now the Smarty setup is ready.

– PHP: Setting up “Smarty Template Engine”

PHP – Setting up “Smarty Template Engine”

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top