Smarty Template principle

Source: Internet
Author: User
Tags smarty template

First, the wording

Generally need the following: Write 3 pages:

1. Display Page aa.html

<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><title>Untitled Document</title></Head><Body><{$a}></Body></HTML>

2. class file Smarty.class.php

<?PHPclassSmarty//write the name of the class{     Public $left= "<{";//represents the left delimiter, when using the Phpcms template, all the code embedded in this page is inside {}. Phpcms default is {}, but with {} is prone to problems, because the static page itself has {}. Here the delimiter is confused with {}. So here is the choice to use <{}>.      Public $right= "}>";//represents the right delimiter, the default is}, the {} to the previous page display, the contents of {} parse out.          Public $attr=Array();//defines an array, which is empty by default. Store the registered variables inside.         functionAssign$name,$value)//the method of registering variables inside the smarty. 2 parameters are required, the first parameter is the variable name, and the second argument is the value of a variable.     {        $this->attr[$name] =$value;//The method calls the array method inside the class, passes the variable name as the index, passes the variable value as the value inside the array, and saves it in the array, which is equivalent to adding a variable to the array.     }        functionDisplay$filename)//The method that invokes the template display, () is the path to the static page.     {        //operation Step//1. Reads all the contents of the static page and saves the contents to the variable str.         $str=file_get_contents($filename); //2. Replace the content, get the content may be the source code of the webpage, will be longer. Locate the contents of the tag, and then replace it. The replacement principle is to find the contents of the <{}> delimiter inside the source code according to the regular expression, and then replace the contents of the content within the array attr with <{}>.        After replacing the content, the replaced page will be stored in the array, there may be variables in the array, such as the CATID, then this page becomes a dynamic page.                3. A strongly replaced page is saved to a location. 4. Load the replaced page into the current page and load it into the current page with include or require.     }}

3. Inject page

<? PHP // This is a pure PHP page, inside only logic, no foreground display code.  $a = ten; include ("./smarty.class.php"); // introducing the Smarty class $smarty New Smarty (); // Create a new Smarty object $smarty->assign ("A",$a); // call the Assign method, which writes a variable with a variable named a and a value of $ A. Register this variable in.  $smarty->display ("aa.html"); // bring a template aa.html in. 

Ii. examples

1. Pages of the class

<?PHPDefine("ROOT",Str_replace("\\","/",dirname(__file__)).‘ /‘);//the root directory of the project is specified in the constant root. Use define to define a constant, the name of the constant is root, the value of the constant is the following content, meaning to get the path of the file, replace the "\" in the Directory with "/". Modify the path inside windows to the path of the file inside the page. echo str_replace ("\ \", "/", DirName (__file__)). ' /‘; Gets the location of the current filerequireROOT. ' Libs/smarty.class.php ';//loading the Smarty class file$smarty=NewSmarty ();//instantiating a Smarty objectDefine("Css_path", "/project/css/");//define constants and place the CSS files under the Project/css folder in the root directory. Define("Js_path", "/project/js/");//define constants and place the JS files under the Project/js folder in the root directory. $smarty-auto_literal = false; You can let the bounding symbol use spaces$smarty->settemplatedir (ROOT. ' Templates/');//set the location of all template files//$smarty->addtemplatedir (ROOT. ' Templates2/'); Add a template folder$smarty->setcompiledir (ROOT. ' Templates_c/');//set the directory where the compiled template resides$smarty->addpluginsdir (ROOT. ' Plugins/');//set as template extension to store directory$smarty->setcachedir (ROOT. ' cache/');//Set cache file storage directory$smarty->setconfigdir (ROOT. ' Configs/');//Setting the template configuration file to store directory$smarty->caching =false;//Setting the smarty cache switch function$smarty->cache_lifetime = 60*60*24;//set the cache template to a valid time of day$smarty->left_delimiter = ' <{';//set the left terminator in the template language$smarty->right_delimiter = '}> ';//set the right terminator in the template language?>

2. Inject page

<? PHP // introducing a configuration file include (".. /init.inc.php "); // init.inc.php has built Smarty objects inside.  $a = "Hello"; $smarty->assign ("Ceshi",$a); // Inject variable $smarty->display ("test.html"); // call the template file to display

3. Display Page

<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><title>Untitled Document</title></Head><Body><{$ceshi}></Body></HTML>

Run the time to run the injection page, injected into the page to display the page to use the display content, not directly go to the display page. Running the display page directly will only show <{$ceshi}>.

Smarty Template principle

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.