PHP (3): Start using smarty_php Tutorial

Source: Internet
Author: User
Tags php compiler
1. About Smartywhen We-doing web programming using PHP, one problem is, the PHP files can being mixed with PHP code As long as the HTML 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 are 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 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? 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 mix Ed 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 som e 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) [PHP] View plaincopyprint? 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 Variab Le 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 "{$ab [b]}" Step 4:write template file (a.html) [HTML] view Plaincopyprint?

{$ab ["B"]}

{$title}{section name=list loop= $title} {$title [List].name}-{$title [list].city}
{/section}The result is like This:as-to-print out of the values in a and dimentional table, we have the use of the {section name= ' Loop =$..} {/section}

http://www.bkjia.com/PHPjc/477816.html www.bkjia.com true http://www.bkjia.com/PHPjc/477816.html techarticle 1. About Smarty when we were doing web programming using PHP, one problem is, the PHP files can was mixed with PHP C Ode as long as the HTML code. At some point, it's not very CLE ...

  • 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.