Most of the time in site development, first the designer makes the interface and breaks into HTML for developer and then developer starts by creating simple scripts to add dynamic features to their Web sites. In real time practice some or more changes are required by client, which increases complexity because of PHP and HTML in same page. And another problem is that developer has to wait until designer completes design and HTML layout.
Why Use Templates?
Template process is quite valid and useful, and most developers who uses this method, agree that the separation of business logic and layout logic makes the code a lot easier to understand and maintain. This is the reason behind templates, to separate business logic from layout.
Advantages of smarty
Smarty is a new development concept in the PHP, and it brings also several new and unique features. One of the mail advantages is that Smarty ‘compiles’ the parsed templates into PHP scripts, and then reuses the compiled template when required. This brings a huge performance improvement over other template solutions, as the main PHP script doesn’t need to parse and output the same template on every request.
Smarty also allows developers to create their own set of functions and have Smarty recognize them. And it also has built-in caching support and special constructs that can be used on templates to control the format of the layout.
And very importantly, Smarty provides developers’ tools that help them separate the business-logic code from the layout-formatting code. And Smarty goes one step further by allowing developers to put control-flow structures in the template source. This might sound a bad idea, since it would imply business-like logic in the template, but it is actually quite useful. You can tell Smarty to use a specific style for any html control on the template itself, instead of having PHP code do this work. After all, this is template-related information.
Installing Smarty
Smarty is quite simple to install. You can get various versions available for download Smarty.
After downloading and extracting the files, copy the resulting Smarty directory to some place inside your include path. A good option is to copy this directory in the PEAR library directory. In UNIX environments, it will usually be:
$ cp -R Smarty /usr/local/lib/php/
In Windows computers, you will need to copy the Smarty directory to ‘C:\php\pear’.
Give write rights to Apache user for template directory and sub directories. This is the directory that smarty uses to store compiled templates.
Using Smarty
Now you have write simple PHP script to manipulate and assign template variable to a value. Like..
<?php
require 'Smarty.class.php';
$smarty = new Smarty;
$smarty->assign("variable_name","Variable Value");
$smarty->display('display.tpl');
?>
Above script includes the Smarty class, which is included inSmarty.class.php
. After that object is created for smarty class and the template variable named$ variable_name
will hold the value "Variable Value
".
At the time of creating templates, the placeholders will be placed as{$variable_name}
, but the placeholder delimiter can also be changed.{$variable_name} will be replaced by its value and output to the browser.
Smarty also compiles the template into a PHP script, so the next time this script is requested, the template will not be parsed again and the compiled PHP script will be used instead.
Summery
Also Control Flow statements, conditions can be used in smarty. Smarty works for designers and developers both and also can reduce development and maintenance time. One has to just go through syntax used for smarty and it will make your work easier.
NOTE :
If you are in need of any Web Development feel free to Inquire us . Dhanashree Inc. Expertise in Asp.net Development, Php Development, Website designing, Open Source customisation. Dhanashree Inc can be our offshore development company / outsourcing web development company, hire dedicated web programmers.
Above information is for knowledge sharing if you have problem / issue / suggestion please intimate us with details for proper and prompt action.