Php ultra-practical template engine-PHP source code

Source: Internet
Author: User
Ultra-practical template engine

Method:
$ This-> assign ('style', $ style); // variable $ this-> display (); // template
 'Mysql', // Database type 'DB _ host' => 'localhost', // server address 'DB _ name' => 'php ', // database name 'DB _ user' => 'root', // username 'DB _ pwd' => '123 ', // password 'DB _ prefix' => 'jiaodu _ ', // database table PREFIX 'DB _ charset' => 'utf8 ', // by default, the database encoding uses utf8/* SESSION settings */'session _ start' => 'user', // SESSION mode, file mode: file, set Database to user/* Template engine settings */'tmpl _ ADMIN_PATH '=> 'admin', // background directory name 'tmpl _ COMPILE_PATH' => '/runtime ', // read/write directory 'tmpl _ path' => '/template', // template PATH 'tmpl _ TEMPLATE_SUFFIX' => 'html ', // the default template file suffix 'tmpl _ L_DELIM '=>' {', // The General tag of the template engine starts to Mark 'tmpl _ R_DELIM' => '}', // tag 'tmpl _ STRIP_SPACE '=> true at the end of the common tag of the template engine. // whether to remove html spaces and line breaks 'tmpl _ CACHE_ON' => true, // whether to enable template compilation cache. if it is set to false, the template will be re-compiled every time. it is generally used for template debugging/* URL setting */'URL _ HTML_SUFFIX '=> 'html ', // Set 'URL _ PATHINFO_MODEL '=> 2 for the URL pseudo-static suffix, // URL mode, 1. do not hide or 2. hide the entry file [rule supported]/* Other settings */'pass _ ENCRYPT '=> 'Www .php1.cn', // encryption factor );
[PHP] code
 Config (); $ this-> templateFile = $ templateFile;} private function config () {global $ config; $ this-> config = $ config; $ this-> CompileDir = $ this-> config ['tmpl _ COMPILE_PATH ']. '/Compile'; $ this-> templateDir = $ this-> config ['tmpl _ path']; $ this-> debuy = $ this-> config ['tmpl _ CACHE_ON '];}/*** check the compilation directory */public function is_CompileDir () {$ dir = APP_PATH. $ this-> CompileDir; if (! Is_dir ($ dir) {if (! Mkdir ($ dir) {die ('automatic creation of compiled directories failed, please create manually ') ;}} if (! Is_writeable ($ dir) {die ('No write permission in the compilation Directory ');}/*** inject variable */public function assign ($ assign) {$ this-> assign = $ assign;}/*** output Template */public function display () {$ this-> is_CompileDir (); $ this-> CompileCheck ();}/*** check compilation */private function CompileCheck () {$ this-> is_CompileDir (); $ filename = APP_PATH. $ this-> CompileDir. '/'. md5 ($ this-> templateFile ). '. php'; if ($ this-> debuy |! Is_file ($ filename) {$ this-> tmplstrtpspace ($ filename);} foreach ($ this-> assign as $ key => $ row) {$ key = $ row;} include $ filename;}/*** format the template and write it into the compilation */private function tmplstrtpspace ($ filename) {if ($ this-> config ['tmpl _ STRIP_SPACE ']) {$ find = array ("~> \ S + <~ "," ~> (\ S + \ n | \ r )~ "); $ Replace = array ("> <","> "); $ tmplContent = preg_replace ($ find, $ replace, $ this-> templateCheck ());} else {$ tmplContent = $ this-> templateCheck ();} if (file_put_contents ($ filename, trim ($ tmplContent) {return true ;} else {die ('Compilation and writing failed') ;}/ *** check template */private function templateCheck () {$ PATH = APP_PATH. $ this-> templateDir. '/'. $ this-> templateFile.'.html '; if (is_file ($ PATH) {return $ this-> template_compile (file_g Et_contents ($ PATH);} else {die ('Template :'. $ this-> templateFile.'.html does not exist ');}/*** compile template */private function template_compile ($ template_Conver) {if (empty ($ template_Conver )) {return $ template_Conver;} else {$ _ Left = $ this-> config ['tmpl _ L_DELIM ']; $ _ Right = $ this-> config ['tmpl _ R_DELIM ']; $ template_Preg [] ='/<\? (= | Php |) (. + ?) \?> /Is '; $ template_Preg [] ='/'. $ _ Left.' (else if | elseif )(.*?) '. $ _ Right.'/I '; $ template_Preg [] ='/'. $ _ Left.' (.*?) '. $ _ Right.'/I '; $ template_Preg [] ='/'. $ _ Left.' while (.*?) '. $ _ Right.'/I '; $ template_Preg [] ='/'. $ _ Left.' (loop | foreach )(.*?) '. $ _ Right.'/I '; $ template_Preg [] ='/'. $ _ Left.' if (.*?) '. $ _ Right. '/I'; $ template_Preg [] = '/'. $ _ Left. 'else '. $ _ Right. '/I'; $ template_Preg [] = '/'. $ _ Left. "(eval | _) (| [\ r \ n]) (. *?) ". $ _ Right. '/is'; $ template_Preg [] = '/'. $ _ Left. '_ e (.*?) '. $ _ Right.'/is '; $ template_Preg [] ='/'. $ _ Left.' _ p (.*?) '. $ _ Right. '/I'; $ template_Preg [] = '/'. $ _ Left. '\/(if | for | loop | foreach | eval | while )'. $ _ Right. '/I'; $ template_Preg [] = '/'. $ _ Left. '(* (\ + | --)*)*? ([_ A-zA-Z] [\ w] * \ (. *? \) | \ $ (\ W +) (\ [| \ () (\ '| ")? \ $ * \ W * (\ '| ")? (\) | \]) * (-> )? \ $? (\ W *) (\ '| ")? (.*?) (\ '| ")? \) |) {0 ,})(*\.? [^ \.] *? *)*?) {1 ,})'. $ _ Right. '/I'; $ template_Preg [] = "/(|) {0,} (\ r \ n) {1 ,}\";/"; $ template_Preg [] = '/'. $ _ Left. '(# | \*)(. *?) (# | \*)'. $ _ Right. '/'; $ template_Preg [] = '/'. $ _ Left. '\ % ([a-zA-Z _ \ x7f-\ xff] [a-zA-Z0-9 _ \ x7f-\ xff] *)'. $ _ Right. '/'; $ template_Replace [] = '<? \ 1 \ 2?> '; $ Template_Replace [] ='
 '; $ Template_Replace [] ='
 '; $ Template_Replace [] ='
 '; $ Template_Replace [] ='
 '; $ Template_Replace [] ='
 '; $ Template_Replace [] ='
 '; $ Template_Replace [] ='
 '; $ Template_Replace [] ='
 '; $ Template_Replace [] ='
 '; $ Template_Replace [] ='
 '; $ Template_Replace [] ='
 '; $ Template_Replace [] = ''; $ template_Replace [] =''; $ template_Replace [] ='
 Lang_array [\ '\ 1 \'];?> '; Return preg_replace ($ template_Preg, $ template_Replace, $ template_Conver );}}}

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.