PHP Smarty Template engine working mechanism (II.)

Source: Internet
Author: User
Tags auth php file php code regular expression smarty template

The so-called compilation is to replace the template with a positive to include the process of PHP code.
It doesn't actually compile every request, so performance is acceptable.

Template files and PHP program files are compiled by the template engine and then combined into a file, the compiled file.

Next, we write a simple template engine based on the principle flow ...

Put the core code first:

Smarty.class.php file

The code is as follows Copy Code
<?php
Class smarty{
Public $template _dir;//Template directory
Public $compile _dir;//Compilation Directory
Public $arr =array ();//define an array to hold the value passed by the second parameter in the Assign
Public function __construct ($template _dir=). /templates ", $compile _dir=". /templates_c ") {
$this->template_dir= $template _dir;
$this->compile_dir= $compile _dir;
}
Public function assign ($content, $replacment =null) {
if ($content!= "") {//If the template variable is specified, the value to be assigned is stored in the array
$this->arr[$content]= $replacment;
}
}
Public function display ($page) {
$tplFile = $this->template_dir. " /". $page;
if (!file_exists ($tplFile)) {
Return
}
$comFile = $this->compile_dir. " /"." Com_ ". $page.". PHP ";
$tplContent = $this->con_replace (file_get_contents ($tplFile));
File_put_contents ($comFile, $tplContent);
Include $comFile;
}
Public Function Con_replace ($content) {
$pattern =array (
'/<{s*$ ([a-za-z_][a-za-z_0-9]*) s*}>/i '
);
$replacement =array (
' <?php echo $this->arr[' ${1} '?> '
);
Return Preg_replace ($pattern, $replacement, $content);
}
}
?>

Smarty.class.php Code Explanation:

$template _dir Specifies the directory for the template file

$compile _dir Specifies the directory of the compiled template file

Constructors

The code is as follows Copy Code

Public function __construct ($template _dir=). /templates ", $compile _dir=". /templates_c ")

{

$this->template_dir= $template _dir;

$this->compile_dir= $compile _dir;

}

By default, the Smarty template engine will use the Templates directory to store template files, Templates_c to store the compiled files

The Assign ($content, $replacment =null) function works by using the statement to pass the value of each variable to be passed to the template:

$this->arr[$content]= $replacment, saved to the array.

So why do you want to save the $replacement value in the array?

In fact, the internal operation is such a process: Save the $replacement value to the array---> read the template file (INDEX.DWT)---> match the values in the array to the variables in the template file (completed by the Con_replace () function)---> Writes the replaced template file to the compiled file (com_index.dwt.php)---> Output compiled PHP file

The dispaly ($page) function receives a parameter, which is the template file to output (INDEX.DWT) First, assigns the path of the template file to $tplfile ($tplFile = $this->template_dir. " /". $page)

To determine if the template file exists, if it does not exist, there is no need to load, direct return

Specify a compile file to hold the template file after the replacement variable

Read the template file through the function file_get_contents () and replace the Smarty label in the template with the function conf_replace ()

Writes the template file after the replacement variable to the compiled file through File_put_contents ()

You can output compiled files by bringing in the compiled file include

function Con_replace ($content) is used to replace a variable in a template file (INDEX.DWT), and the variable value in PHP is assigned to a variable in the template to match the contents of the template file with a regular expression that matches the <{$title}> form. and replaces the matching value with the form of the <?php echo $title?>

Match to content and return the replaced content

The code is as follows Copy Code

/*smarty.ini.php file: To complete the initialization of Smarty
<?php
Include "./libs/smarty.class.php";
$TPL =new Smarty ();
$tpl->template_dir= "./tpl";
$tpl->compile_dir= "./compile";
?>

<!--template file-->
<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title><{$title}></title>
<body>
<p> content: <{$content}></p>
<p> Author: <{$auth}></p>
<p> website: <{$website}></p>
</body>

Copy Code

  code is as follows copy code

/*index.php file */
<?php
    include. Smarty.ini.php ";
    $title = "Smarty template engine working mechanism";   
    $content = " Smarty template engine working mechanism flow chart ";
    $auth = "Marcofly";
    $website = "www.MarcoFly.com";
    $tpl->assign ("title", $title);
    $tpl->assign ("content", $content);   
    $tpl-> Assign ("auth", $auth);
    $tpl->assign ("website", $website);
    $tpl->display ("index.dwt");
?

The index.php is written by a PHP programmer who can get all the data you want from the database and save it to a variable, and then simply call the Assign () function to save the data in an array and output the file through the display () function

Note: This compiled file is a PHP file that executes through the server side and outputs the results to the browser on the client

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.