What is Smarty
Smarty is a templating engine for PHP. It allows you to separate logic and presentation by separating the PHP code from the HTML (or anything else for that matter) presentation.More specifically, it facilitates a manageable way to separate application logic and content from its presentation. This is best described in a situation where the application programmer and the template designer play different roles, or in most cases are not the same person.
Smarty generates web content through the placement of special Smarty tags within a document. These tags are processed and substituted with other code. Tags are directives for Smarty that are enclosed by template delimiters. These directives can be variables, denoted by a dollar sign ($), functions, logical or loop statements. Smarty allows PHP programmers to define custom functions that can be accessed using Smarty tags.
The business logic to use the Smarty template above could be as follows:
Custom templating engine example
There are two files here "mail.html" (we can say this is a template file) and "mail_engine.php" (ya, you are right...it
is an engine.)
mail.html
mail_engine.php
This example allows small functionality. There are much more advantages of using smarty.
Some of Smarty's features:
It is extremely fast.
It is efficient since the PHP parser does the dirty work.
No template parsing overhead, only compiles once.
It is smart about recompiling only the template files that have changed.
You can easily create your own custom functions and variable modifiers, so the template language is extremely extensible.
Configurable template {delimiter} tag syntax, so you can use {$foo}, {{$foo}}, , etc.
The {if}..{elseif}..{else}..{/if} constructs are passed to the PHP parser, so the {if...} expression syntax can be as simple or as complex an evaluation as you like.
Allows unlimited nesting of sections, if's etc.
This example allows small functionality. You might have big problems if your company value contains #CITY#, for example. There are much more advantages of using smarty. But anyway, raw PHP templating is most effective and fastest
How does it work
Smarty is a template engine which actually compiles the template file to the php file that can be later executed. This simply saves time on parsing and variable outputs, beating other Template Engines with much smaller memory use and regex.
Installation
Download Smarty Source from smarty.net
Open it using your archive extractor (must be compatible with .tar.gz files)
Go to the directory called Smarty-a.b.c
Copy the libs folder to your website's root ( where you want the website to exist, for example /My_Site/ ) You are done
Simple example
$abc = 'hello ';
$smarty->abc("abc",$abc);
{$abc}
Basic Syntax
{ Sample Smarty Template }
{ Include a header file }
{ Include a file from a variable $header_file, which is defined by the php script } {include file=$header_file} {include file="middle.tpl"} { Simple alternative to PHP's echo $title; {$title} { Include a file from a variable #footer#, which is defined by the config file *} {include file=#footer#}
{ display dropdown lists } {end}
Comments:
{ Comment }
Writing a variable, assigned from the PHP script:
{$variable}
Writing a variable, assigned from the config file:
#variable#
Using a variable in a function:
$variable
Integrating into a website
template_dir="templates"; // From here you should put your own code // Set some variables $smarty->assign("Title"=>"Just a test"); // Create an array, which we will assign later $contacts=array( array("Name"=>"John Parkinson","email"=>"john.parkinson.test@domain.tld","age"=>26), array("Name"=>"Super Mario","email"=>"super.mario@domain.tld","age"=>54), array("Name"=>"Pete Peterson","email"=>"pete.peterson@domain.tld","age"=>18), array("Name"=>"Smarty Creator","email"=>"smarty.creator@domain.tld","age"=>37) ); // Assign the array $smarty->assign("contacts",$contacts); // Compile and Display output of the template file '''templates/index.tpl''' // Up to here you should put your own code $smarty->display("index.tpl"); ?>Looping
Looping in smarty is just like PHP, except that there are different ways of approaching the variables. For example, in PHP you would write this:
foreach($array as $key => $value) { echo "$key => $value\n"; }
As Smarty compiles similar piece of code by this:
{foreach from=$array key="key" item="value"} {$key} => {$value} {/foreach}
Also, you can use a section function which is very similar to foreach
In the case the designer wanted to add bullet points, or indexes of the array items, there would be no need to do anything for the programmer as you can use other variables that would change themselves after each loop.