Smarty Framework in PHP

Smarty is a web template engine which is a presentation of HTML & CSS parts. Smarty framework totally depends on PHP OOPS technology. Smarty is not made of MVC architecture and Zend Framework, CodeIgniter, CakePHP, or any of the other Application development frameworks for PHP. When we display any value, we store the value into the variable and this variable passes into a file like index.tpl. The Smarty templating file extension is .tpl file .
Smarty function

This is a method of Smarty which is used to assign a value into the variable and this variable passes variable to tpl file. Assign two arguments, where first is the name with the variable name of the template file and the second is the original data.

This is the method of Smarty which is used to display and load the template .tpl file that is passed to it.

Advantages of Smarty
  • Clean code presentation.
  • PHP back end and front end Smarty template.
  • Doesn't replace PHP but complements it.
  • Fast development/deployment for the programmers and the designers
  • Quick and easy to maintain.
  • Syntax is easy to understand, no PHP knowledge is required.
  • Flexibility for custom development.
  • Security: insulation from PHP.
  • Free, open source.


Smarty syntax is used to display symbols like {$title|escape|lower}, shown below:

  1. include('Smarty.class.php');  
  2. // create object  
  3. $smarty = new Smarty;  
  4. // assign some content. This would typically come from  
  5. // a database or other source, but we'll use static  
  6. // values for the purpose of this example.  
  7. $smarty->assign('name''george smith');  
  8. $smarty->assign('address''45th & Harris');  
  9. // display it  
  10. $smarty->display('index.tpl')  

Let's start Smarty framework. Today, I will discuss an array display into Smarty framework.

First, download Smarty folder by reference site smarty download for the latest version of Smarty.

The Downloaded image is shown above.

Maintenance release is depicted above.

Click source code and download Smarty framework. When downloading is complete, then downloading folder extracts and copies the Smarty folder, which is put into the xampp->htdoc folder.

First Step

To create index.php file into the Smarty folder, all folders,  and the file remove the without libs folder.

Step Second

Open index.php file. 

  1. <?php  
  2. /** 
  3.  * Example Application 
  4.  * 
  5.  * @package Example-application 
  6.  */  
  8. require 'libs/Smarty.class.php';  
  10. $smarty = new Smarty;  
  12. //$smarty->force_compile = true;  
  13. $smarty->debugging = true;  
  14. $smarty->caching = true;  
  15. $smarty->cache_lifetime = 120;  
  17. $smarty->assign("Name""Fred Irving Johnathan Bradley Peppergill", true);  
  18. $smarty->assign("FirstName"array("John""Mary""James""Henry"));  
  19. $smarty->assign("LastName"array("Doe""Smith""Johnson""Case"));  
  20. $smarty->assign("Class"array(array("A""B""C""D"), array("E""F""G""H"),  
  21.                                array("I""J""K""L"), array("M""N""O""P")));  
  23. $smarty->assign("contacts"array(array("phone" => "1""fax" => "2""cell" => "3"),  
  24.                                   array("phone" => "555-4444""fax" => "555-3333""cell" => "760-1234")));  
  26. $smarty->assign("option_values"array("NY""NE""KS""IA""OK""TX"));  
  27. $smarty->assign("option_output"array("New York""Nebraska""Kansas""Iowa""Oklahoma""Texas"));  
  28. $smarty->assign("option_selected""NE");  
  30. $smarty->display('index.tpl');  
Now create configs folder. Its used for the database configuration.

Plugins folder: It is used for a third party code and plugin.

Templates: It is the last and final folder and it is used for all tpl files into the templates folder.