Installing Smarty™ – For Windows Environment
A step-by-step guide using Smarty – 2.6.22
About Smarty:
Smarty is a market leader in providing template engine for PHP. Smarty facilitates in separating page layout code of your application from the logical code (as demonstrated later in this guide). This is helpful when page layout is handled by a separate team and logical coding by other team. This makes both teams work independently.
Prerequisites:
1. PHP 5x should be installed and running on your machine.
2. Apache 2x should be installed and well running on your machine.
3. You should have downloaded file Smarty-2.6.22.zip. The same can be download from http://www.smarty.net/
Assumptions:
This guide assumes that the user is comfortable with below mentioned technologies and is looking forward to enhance his PHP development skills with the help of Smarty™.
1. Windows environment
2. PHP Language
3. XHTML/HTML markup language
Installation Guide:
• Directory Category & Architecture
For Smarty to work well we will be making various directories during installation and for making demo application. These directories can be categorized into three different categories -
1. Smarty Template Engine Libraries – It holds library files provided by smarty to implement smarty template engine.
2. Template, and Config directories (comprising of 4 directories) – These set of directories are used for keeping the design template, configuration files, and template compiled code.
3. Application directory – This directory holds your application and related files.
There are four different directory structures you can follow to install and use Smarty (as below). We will be following Structure 1 for our Installation Guide.
Structure 1 – In this implementation structure Smarty Library files and directories are installed inside the web application your are making. All the other directories used for holding templates and compiled files are placed inside the smarty library folder. The advantage of this implementation is that your web application becomes the holder of complete smarty installation. This is helpful in case you plan to move to newer server.

Structure 2 – As explained the advantage of Structure 1 this structure also holds the same advantage. The only difference is that you may want to separate the template and Config Directories from Smarty template engine. This is beneficial in case you want to upgrade your Smarty or smarty library files got corrupted.

Structure 3 – In this structure all the three categories of folders are kept separately. They are bound by the coding.

Structure 4 – This structure separates Application from Smarty installation and support folders. This may be useful to those web applications where changes are done frequently and hosting is performed by may developers. Using this implementation the developers may remain focused on their hosting without any fear of corrupting Smarty setup.

• Installation Steps
Installing Smarty™ is a simple two step process –
1. Installing Smarty Library Files
2. Setting up Smarty Directories
Now, let’s begin with our first step i.e., Installing Smarty Library files –
1. Install Smarty™ Library Files
a. You have already downloaded the setup file of Smarty in .zip format.
b. Make a directory in desired drive with the name /smarty_setup/.
c. Unzip the file into this directory.
Now you have smarty setup file and directories in the newly made directory.
d. Make a directory with the name of /mysmartysite/ in Apache Web Server /htdocs/ (this directory is a default directory used for hosting web sites in Apache Web Server). Make another directory with the name of /smarty/ in /mysmartysite/ directory.
So, now you have /htdocs/mysmartysite/smarty/ path available.
e. Copy all files and directories from /smarty_setup/Smarty-2.6.22/libs/ directory and paste it in /htdocs/mysmartysite/smarty/.
You should now have the following file structure:
/mysmartysite/smarty/
Config_File.class.php
debug.tpl
Smarty.class.php
Smarty_Compiler.class.php
/internals/
/plugins/
Let’s now move to next set of installation i.e., Setting up of Smarty Directories.
2. Setting up Smarty™ Directories
a. Open /htdocs/mysmartysite/smarty/ directory.
b. Create directory with the name of
/templates/
/templates_c/
/cache/
/configs/
You should now have the following file structure:
/mysmartysite/smarty/
Config_File.class.php
debug.tpl
Smarty.class.php
Smarty_Compiler.class.php
/internals/
/plugins/
/templates/
/templates_c/
/cache/
/configs/
This completes your Smarty installation process. It’s time to test the same by making a sample application.
Making First Application:
Smarty being a template engine helps you in dividing your old code into two different parts namely the Design part, and the Coding part. We will now demonstrate both the parts. You can copy the given code and save it with the given file name or you can download the same from http://www.growthtechnosoft.com/techpaper.htm
• Coding PHP file
Open your desired editor, type the following code in the file and save it as first.php in /htdocs/mysmartysite/
first.php
<?php
// importing the Smarty class
require_once('./smarty/Smarty.class.php');
// now make an object of the class
$newObj = new Smarty();
// mapping smarty directories
$newObj ->template_dir = './smarty/templates';
$newObj ->compile_dir = './smarty/templates_c';
$newObj ->cache_dir = './smarty/cache';
$newObj ->config_dir = './smarty/configs';
// assigning values to variables and displaying using template
$newObj ->assign('name', 'Manu');
$newObj ->display('first.tpl');
?>
• Coding Design Template
Now make a template file by opening editor again and type the below code in it and save it as first.tpl in /htdocs/mysmartysite/smarty/templates/
first.tpl
<html>
<head>
<title>First Smarty Template</title>
</head>
<body>
<p>Hello, {$name}!</p>
</body>
</html>
With this your coding job is over. Let’s now run the application and see how Smarty works.
Open the browser and type the URL http://localhost/mysmartysite/first.php. You will see the output on your screen “Hello Manu”. The Smarty template engine has used your desired template mentioned in your php code at line 16 and displayed the output. Let us now make another template and change the name of template in php code file and check the effect.
Opening the editor again, type the below code and save it as new.tpl in /htdocs/mysmartysite/smarty/templates/
new.tpl
<html>
<head>
<title>New Smarty Template</title>
</head>
<body>
<table border='1'>
<tr>
<td>Hello, {$name}! How are you today</td>
</tr>
</table>
</body>
</html>
Now make changes in php code at line number 16 and replace
$newObj ->display('first.tpl');
With -
$newObj ->display('new.tpl');
Save it and again type the URL http://localhost/mysmartysite/first.php. You will now see a new look with border and new message.
Hope, you enjoyed this document and learned the Installation of Smarty.
Other Resources:
• Official site of Smarty is http://www.smarty.net/
• Sample applications and documentation http://www.growthtechnosoft.com/techpaper.htm
Feedback:
We would appreciate any constructive feedback on this document. Please let us have your Suggestions, Corrections, or Modification at the given email.
Manu Gupta
contact@growthtechnosoft.com
More Papers...
|