PHP (3): Start using Smarty
1. About Smarty
When we were doing web programming using PHP, one problem is, the PHP files can was mixed with PHP code as long as the H Tml code. At some point, it's not very clean and also not safe. And the work can ' t is seperated for back-end programmers and front-end programmers. So we need a tool to seperate the PHP logic from the HTML code to well organize the and maintain the developing process. So here it comes Smarty.
Smarty is a web Template system written in PHP. Smarty is primarily promoted as a tool for separation of Concerns. [ 1 ] Smarty is intended to simplify Compartmentalization, allowing the presentation of a Web page to change separately from the back-end. Ideally, this eases the costs and efforts associated with software maintenance.
Smarty generates web content by the placement of special Smarty tagswithin a document. These tags is processed and substituted with the other code. Tags is directives for Smarty that is 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 the can be accessed using Smarty tags.
2. Set up Smarty
Step1:download the Smarty and rename it ' s Libs folder and import it into our PHP project.
Step2:create a PHP file to connect to Smarty (smartycon.php)
Config_dir = "smarty/"; Smarty S Config info $smarty->caching = false; Use the cache or not $smarty->template_dir = "./templates"; Set the folder for keeping the templates/** * Smarty can automatically compile the templates and PHP contents to an Mixed file * and is stored in Templates_c folder */ $smarty->compile_dir = "./templates_c"; The folder that store compiled files $smarty->cache_dir = "./smarty_cache"; Store cache files $smarty->left_delimiter = "{"; $smarty->right_delimiter = "}";?>
According to the code we should also create 3 folders which is used to store some corresponding files. Templates folder is used to store HTML files which as the folder's name Shows:they is templates, and would be called by " $smarty->display () "to show different styles for a project. Tempates_c is used to store the compiled files. PHP files and templates is written in different files, but the PHP compiler can compile the templates and PHP contents to An mixed file and store them into Templates_c folder. Smarty_cache is used to store cache files.
Step3:write the PHP content (a.php)
Assign ("title", $name); Assign PHP Variabel to the tab in Templates//$smarty->display ("a.html"); Show the template $nameTwo [] = Array ("name" = "Jimmy", "City" = "Montreal"); $nameTwo [] = Array ("name" = "Tim", "City" = "Wuxi"); $nameTwo [] = Array ("name" = "Sam", "City" = "newyork"); $nameTwo [] = Array ("name" = "John", "City" = "SanFran"); $nameTwo [] = Array ("name" = "Lily", "City" = "Loyola"); $title = Array ("a" + = "name", "B" = "News", "c" = "date", "D" and "Now ()"); $smarty->assign ("title", $nameTwo); Assign PHP Variabel to the tab in Templates $smarty->assign ("AB", $title); $smarty->display ("a.html"); Show the Template?>
As the code Abouve shows, we can use $smarty->assign ("AB", $title), we can assign a PHP variable to a smarty variable. Then we use $smarty->display () to display the corresponding template. In the template file we havce to use the same assigned file to display the value of the PHP content here. For example, if we want the template show $title S "News". In a.html file we have the use "{$ab [b]}"
Step 4:write template file (a.html)
{$ab ["B"]}
{$title} {section name=list loop= $title} {$title [List].name}-{$title [list].city}
{/section}
The result is a like this:
The values in a and dimentional table of the ' to ' and ', ' and ' to ', ' name= ' loop=$. {/section}