Analysis on the principle of the smarty template engine

Source: Internet
Author: User

// PHP controller File

<? PHP
// Introduce the template engine File
Include ("201304.php ");
$ Smarty = new tinysmarty ();
$ Qq_numbers = array ('a1' => '20180101', 'a2 '=> '20180101', 'a3' => '20180101 ', 'a4 '=> '123 ');
$ Smarty-> assign ($ qq_numbers );
$ Smarty-> assign ('title', 'This is my QQ number ');
$ Smarty-> assign ('contents', 'This Is My QQ: 100 ');
$ Smarty-> display('20120305_01.html ');

?>

 

Template engine type: 20130304.php

<? PHP
/***
Smarty template engine principle
1: Read the Template File
2: replace the template tag with PHP executable code
3: Save the replaced PHP File
***/
/*
Question?
1: Is every access compiled a waste of CPU?
The compilation file exists, so you do not need to introduce it directly during compilation.
2: After the template file is modified, it must be re-compiled.
When the modification time of the template file exceeds the modification time of the compilation file, it indicates that the template file has been modified,
Therefore, recompile the template file.
*/
Class tinysmarty {
// Template file storage directory
Public $ template_dir = "./templates /";
// Directory for storing compiled files
Public $ compile_dir = "./c_templates /";

// Store variable values
Public $ tpl_vars = array ();

// Assign
// Store the variable as an array to the $ tpl_var attribute
Public Function assign ($ tpl_var, $ Var = NULL ){
// Input an array to assign values in batches
If (is_array ($ tpl_var )){
Foreach ($ tpl_var as $ _ key => $ _ Val ){
If ($ _ key! = ''){
$ This-> tpl_vars [$ _ key] = $ _ Val;
}
}
} Else {
// Pass in non-null characters
If ($ tpl_var! = ''){
$ This-> tpl_vars [$ tpl_var] = $ var;
}
}
}

/*
Name display
Param string $ tpl_file file name

*/
Public function display ($ tpl_file ){
// Template file path
$ Template_file_path = $ this-> template_dir. $ tpl_file;
// Compile the file path
$ Compile_file_path = $ this-> compile_dir. $ tpl_file;
// Determine whether the compiled file exists
If (! File_exists ($ compile_file_path) | filemtime ($ template_file_path)> filemtime ($ compile_file_path )){
// Determine whether a file exists
If (! File_exists ($ template_file_path )){
Return false;
}
// Read the file content
$ Fpl_file_con = file_get_contents ($ template_file_path );

// Replace the template tag
// Replace {$ title} with <? PHP echo $ title ;? >
// Regular Expression // here the regular expression involves regular response references
$ Pattern = '/{\ s * \ $ ([_ A-Za-Z] [_ 0-9a-za-z] *) \ s * \}/I ';
$ Replace = '<? PHP echo $ this-> tpl_vars ["$ {1}"];?> ';
$ New_file_con = preg_replace ($ pattern, $ replace, $ fpl_file_con );
// Write File Content
File_put_contents ($ compile_file_path, $ new_file_con );
}

// Introduce the compiled file
Include ($ compile_file_path );
}
}

?>

 

Template File 20120305_01.html

<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title >{$ title }</title>
</Head>
<Body>
<H1> hello-{$ Contents}
</Body>
</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.