PHP (3): Start using Smarty

Source: Internet
Author: User

1. about SmartyWhen we are doing web programming using PHP, one problem is that the php files can be mixed with php code as long as the html code. at some point, it is not very clean and also not safe. and the work can't be 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 pro Cess. 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 plac Ement of special Smarty tagswithin 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. 2. set up SmartyStep1: 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) [php] view plaincopyprint? <? Php/** Created on Jan 10,201 3 * Author: Nick * Function: Connecting to Smarty */include_once ("smarty/Smarty. class. php "); $ smarty = new Smarty (); // new an instance of smarty $ smarty-> config_dir =" smarty /"; // smarty's config info $ smarty-> caching = false; // use cache or not $ smarty-> template_dir = ". /templates "; // set the folder for keeping the templates/*** smarty can be automatically compile th E templates and php contents to an mixed file * and be 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 shoshould also create 3 folders which are used to store some corresponding files. templates folder is used to store html files which as the folder's name shows: they are templates, and will 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 are written in different files, but the ph P 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. step 3: write the php content (. php) [php] view plaincopyprint? <? Php/** Created on Jan 10,201 3 * Author: Nick * Function: */include ("SmartyCon. php "); $ name =" php100 "; // $ smarty-> 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"); $ name Two [] = array ("name" => "john", "city" => "sanfran "); $ nameTwo [] = array ("name" => "lily", "city" => "loyola"); $ title = array ("a" => "name ", "B" => "News", "c" => "date", "d" => "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 to use "{$ AB [B]}" Step 4: Write template file (a.html) [html] view plaincopyprint? <Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.