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
Assign
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.
Display
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.
Syntax
Smarty syntax is used to display symbols like {$title|escape|lower}, shown below:
- include('Smarty.class.php');
-
- $smarty = new Smarty;
-
-
-
- $smarty->assign('name', 'george smith');
- $smarty->assign('address', '45th & Harris');
-
- $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.
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.
- <?php
-
-
-
-
-
-
- require 'libs/Smarty.class.php';
-
- $smarty = new Smarty;
-
-
- $smarty->debugging = true;
- $smarty->caching = true;
- $smarty->cache_lifetime = 120;
-
- $smarty->assign("Name", "Fred Irving Johnathan Bradley Peppergill", true);
- $smarty->assign("FirstName", array("John", "Mary", "James", "Henry"));
- $smarty->assign("LastName", array("Doe", "Smith", "Johnson", "Case"));
- $smarty->assign("Class", array(array("A", "B", "C", "D"), array("E", "F", "G", "H"),
- array("I", "J", "K", "L"), array("M", "N", "O", "P")));
-
- $smarty->assign("contacts", array(array("phone" => "1", "fax" => "2", "cell" => "3"),
- array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234")));
-
- $smarty->assign("option_values", array("NY", "NE", "KS", "IA", "OK", "TX"));
- $smarty->assign("option_output", array("New York", "Nebraska", "Kansas", "Iowa", "Oklahoma", "Texas"));
- $smarty->assign("option_selected", "NE");
-
- $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.
Output