Sunday 3 June 2012

Smarty Template Installation

Smarty Template Step By Step Tutorial

I had a hard time installing smarty template engine for PHP on Windows. Here is the detail/simple procedure I found after a lots of testing. Basically installing Smarty is simple but no one had given prerequisite for installation. I feel PHP and Wamp Server installation as the prerequisite for Smarty. Once your PHP and Wamp is ready your job is very simple with smarty.

Note: Before installing smarty template install PHP and Wamp Server for details check my post for PHP and Wamp  with out  proper installation of PHP and Wamp Server Smarty wouldn't provide the desired results.
  1. Prepare directory for practice, example D:\wamp\www\test\smarty (it is must that you install wamp on D or E colon in your windows 7 Machine).
  2. Download file Smarty Template. You can download from their site at smarty.php.net. Place within practice directory. Thus, we will get (example), www\test\smarty\Smarty-3.1.8.tar.gz
  3. Extract the smarty compressed file. we will get folder named "Smarty-3.1.8.".
  4. Rename that folder to"Smarty".
  5. Create two folders named: template and templates_c. Now, you get structure like this:

we try to test it. We want to create a page to show simple hello text. Let's do it.

First, Create a file named "test.tpl" within template folder. Enter following code:

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


Next, create a php file named "test.php" within D:\wamp\www\test\smarty . Enter following code:

<?php 
require 'Smarty/libs/Smarty.class.php'; 

$smarty = new Smarty; 
  
$smarty->assign('title','Hello World'); 
$smarty->assign('hello','Hello World, this is my first Smarty!'); 
$smarty->display('template/test.tpl'); 
?>


This is our structure:



Now, test it by pointing your browser to http://localhost/test/smarty/test.php. You should get the following result:


No comments:

Post a Comment