What is Smarty ?

Smarty is a set of PHP classes that compile the templates into PHP scripts. Smarty is a template language and it managed to segregate the 'Logical Part (Programming Part)' and 'Presentation Part (Design Part)'.

How 'Logical Part' segregate from 'Presentation Part' ?

In theoretical web development process is that: first the designer makes the interface, and breaks it down into HTML pieces for the programmer then the programmer implements the PHP business logic into the HTML.But in practical point of view, the client frequently comes with more requirements, or maybe more modifications to the design or to the business logic. When this happens , the HTML is modified (or words rebuilt ) programmer changes the code inside HTML.The problem with this scenario is that the programmer needs to be on stand-by until the designer completes the layout and the HTML files. Another problem is that if there is a major design change then the programmer will change the code to fit in the new page. And that's why Smarty is used.

Example :

Let's say a programmer or a designer is creating a web page that is consisting of some data & the all data are categorized in different formats i.e. header, body (menu & menu contents) & footer. The programmer retrieves the data into the template file i.e. (HTML) using smarty. Here the programmer need not to be worry about the designing looks in the template. Then the template designer edits the templates using HTML tags to show the presentation of these elements (HTML tables, background colors, font sizes, style sheets, etc.). One day the programmer needs to change in the programming part. This change does not affect the templates. The content will still arrive in the template as it was. Similarly if the template designer wants to redesign the templates, it wont affect the programming part.

Why is separating PHP from templates important?

1. FOR EASY SYNTAX :

Templates typically consist of semantic markup such as HTML. PHP syntax works well for application code, but quickly degenerates when mixed with HTML.
Smarty's simple {tag} syntax is designed specifically to express presentation. Smarty focuses your templates on presentation and less on "code".
This lends to quicker template deployment and easier maintenance. Smarty syntax requires no working knowledge of PHP, and is intuitive for programmers and non-programmers alike.

2. FOR INSULATION

When PHP is mixed with templates, there are no restrictions on what type of logic can be injected into a template. Smarty insulates the templates from PHP, creating a controlled separation of presentation from business logic. Smarty also has security features that can further enforce restrictions on templates.

Advantages

1. Separation of PHP from HTML/CSS just makes sense
2. Security for 3rd party template access
3. Extremely fast and efficient
4. No template parsing over head, because only compiles once
5. Extensible because the user can create his own custom function

How does it works ?

Smarty parses the templates and creates PHP scripts. When a web page is accessed, PHP executes the compiled smarty file instead of pulling the templates, which saves the work of having to parse the templates again. It only re-compiles the templates when any changes are occur to them, that's why we don't have to manually compile the templates.

INSTALLATION

1. Install the Smarty library files which are in the /libs/ sub directory of the distribution. These are .php files that you SHOULD NOT edit.
2. Smarty uses a PHP constant named SMARTY_DIR which is the full system file path to the Smarty libs/ directory. Basically, if your application can find the Smarty.class.php file, you do not need to set the SMARTY_DIR as Smarty will figure it out on its own. Therefore, if Smarty.class.php is not in your include_path, or you do not supply an absolute path to it in your application, then you must define SMARTY_DIR manually.
3. Create an instance of smarty in your PHP scripts

NOTE : Smarty has a capital 'S'
Syntax <?php
require_once('Smarty.class.php');
$smarty = new Smarty();
$smarty->template_dir='/var/www/html/tutorials/smarty/templates';
$smarty->compile_dir='/var/www/html/tutorials/smarty/templates_c';
return $smarty;
?>

If Smarty.class.php file is not found, then SMARTY_DIR constant set manually.

Syntax <?php define('SMARTY_DIR', '/path/');
require_once(SMARTY_DIR . 'Smarty.class.php');
$smarty = new Smarty();
$smarty->template_dir='/var/www/html/tutorials/smarty/templates';
$smarty->compile_dir='/var/www/html/tutorials/smarty/templates_c';
return $smarty;
?>
Example :
				In PHP side:-
				function getSmarty(){
				  define("SMARTYDIR","/var/www/html/Smarty/");
				  require(SMARTYDIR.'libs/Smarty.class.php');
				  $smarty=new Smarty;
				 $smarty->template_dir='/var/www/html/tutorials/smarty/templates';
				 $smarty->compile_dir='/var/www/html/tutorials/smarty/templates_c';
				 return $smarty;
				 }
				 $smarty=getSmarty();
				 $smarty->assign('fname','Afixi');
				 $smarty->display('show.tpl.html');
				?>
				In htmlside:-
				{$fname}
	    

Demo:

After editing Click here:
Php Code :
Output:
Hello World
Smarty Code :