Add the phpcms template mechanism to CodeIgniter _ PHP Tutorial

Source: Internet
Author: User
Add the phpcms template mechanism to CodeIgniter. CodeIgniter is suitable for application development on small sites, but its built-in view function may cause troubles for front-end personnel who do not understand PHP. In contrast, the view template parsing of phpcms is much more powerful, and CodeIgniter is suitable for small site application development. However, its built-in view function may cause trouble for front-end personnel who do not understand PHP. In contrast, the view template parsing of phpcms is much more powerful, so the template parsing function of PHPCMS is removed here and added to PHPCMS.

First, add template_cache.php to CodeIgniter libraries.

 Cache_path = APPPATH. 'views ';} /*** compile template ** @ param $ module name * @ param $ template file name * @ param $ istag whether it is a tag template * @ return unknown */public function template_compile ($ module, $ template, $ style = 'default') {$ tplfile = APPPATH. 'view '. DIRECTORY_SEPARATOR. $ module. DIRECTORY_SEPARATOR. $ template. '. php'; if (! File_exists ($ tplfile) {show_error ($ tplfile, 500, 'Template does not exist (1) ');} $ content = @ file_get_contents ($ tplfile ); $ filepath = $ this-> cache_path.DIRECTORY_SEPARATOR. 'caches _ template '. DIRECTORY_SEPARATOR. $ style. DIRECTORY_SEPARATOR. $ module. DIRECTORY_SEPARATOR; if (! Is_dir ($ filepath) {mkdir ($ filepath, 0777, true);} $ compiledtplfile = $ filepath. $ template. '. php'; $ content = $ this-> template_parse ($ content); $ strlen = file_put_contents ($ compiledtplfile, $ content); chmod ($ compiledtplfile, 0777 ); return $ strlen;}/*** Update template cache ** @ param $ tplfile template original file path * @ param $ compiledtplfile after compilation, write file name * @ return $ strlen length */public function template_refresh ($ tplfile, $ compiledtplfile) {$ str = @ file_get_contents ($ tplfile ); $ str = $ this-> template_parse ($ str); $ strlen = file_put_contents ($ compiledtplfile, $ str); chmod ($ compiledtplfile, 0777); return $ strlen ;} /*** resolution template ** @ param $ str template content * @ return ture */public function template_parse ($ str) {$ str = preg_replace ("/\ {template \ s + (. + )\}/","
 ", $ Str); $ str = preg_replace ("/\ {include \ s + (. + )\}/","
 ", $ Str); $ str = preg_replace ("/\ {view \ s + (. + )\}/","
 Load-> view (\ 1);?> ", $ Str); $ str = preg_replace ("/\ {php \ s + (. + )\}/","
 ", $ Str); // alex fix $ str = preg_replace (" // {if \ s + (. + ?) \}}/"," ''If \ 1 ''", $ str); $ str = preg_replace ("// \{{ else \}}/", "''else'", $ str); $ str = preg_replace ("// {{\/ if \}}/", "''/if ''", $ str); $ str = preg_replace ("/\ {if \ s + (. + ?) \}/","
 ", $ Str); $ str = preg_replace ("/\ {else \}/","
 ", $ Str); $ str = preg_replace ("/\ {elseif \ s + (. + ?) \}/","
 ", $ Str); $ str = preg_replace ("/\ {\/if \}/","
 ", $ Str); // for loop $ str = preg_replace ("/\ {for \ s + (. + ?) \}/","
 ", $ Str); $ str = preg_replace ("/\ {\/\}/","
 ", $ Str); // + -- $ str = preg_replace ("/\ {\ + (. + ?) \}/","
 ", $ Str); $ str = preg_replace ("/\ {\-(. + ?) \}/","
 ", $ Str); $ str = preg_replace ("/\ {(. + ?) \ + \}/","
 ", $ Str); $ str = preg_replace ("/\ {(. + ?) \-\-\}/","
 ", $ Str); // alex fix $ str = preg_replace ("/\ ''if \ s + (. + ?) \ ''/", "{If \ 1}", $ str); $ str = preg_replace ("/\ ''else ''/", "{else }}", $ str); $ str = preg_replace ("/\ ''\/if \''/"," {/if }}", $ str); $ str = preg_replace ("/\ {loop \ s + (\ S + )\}/","
 ", $ Str); $ str = preg_replace ("/\ {loop \ s + (\ S +) \}/","
 \ 3) {?> ", $ Str); $ str = preg_replace (" // \{\/ loop \}/","
 ", $ Str); $ str = preg_replace ("/\ {([a-zA-Z _ \ x7f-\ xff] [a-zA-Z0-9 _ \ x7f-\ xff:] * \ ([^ {}] *) \}/","
 ", $ Str ); $ str = preg_replace ("// {\\$ ([a-zA-Z _ \ x7f-\ xff] [a-zA-Z0-9 _ \ x7f-\ xff:] * \ ([^ {}] *) \}/","
 ", $ Str ); $ str = preg_replace ("/\ {(\ $ [a-zA-Z _ \ x7f-\ xff] [a-zA-Z0-9 _ \ x7f-\ xff] *) \}/","
 ", $ Str); $ str = preg_replace ("/\ {(\ $[a-zA-Z0-9 _ \ [\] \ '\ "\ $ \ x7f-\ xff] +) \}/es "," \ $ this-> addquote ('
 ') ", $ Str); $ str = preg_replace ("/\ {([A-Z _ \ x7f-\ xff] [A-Z0-9 _ \ x7f-\ xff] *) \}/s ","
 ", $ Str); $ str = preg_replace ("/\ {pc :( \ w +) \ s + ([^}] +) \}/ie "," self:: pc_tag ('$ 1',' $ 2', '$ 0') ", $ str ); $ str = preg_replace ("/\ {\/pc \}/ie", "self: end_pc_tag ()", $ str); $ str ="
 ". $ Str; return $ str ;} /*** escape // The/*** @ param $ var escape character * @ return the escape character */public function addquote ($ var) {return str_replace ("\" "," \ "", preg_replace ("/\ [([a-zA-Z0-9 _\-\. \ x7f-\ xff] +) \]/s "," ['\ 1'] ", $ var ));} /*** resolve the PC tag * @ param string $ op operation method * @ param string $ data parameter * @ param string $ all html code matched by HTML */public static function pc_tag ($ op, $ data, $ html) {preg_match_all ("/([ -Z] +) \ = [\ "]? ([^ \ "] +) [\"]? /I ", stripslashes ($ data), $ matches, PREG_SET_ORDER); $ arr = array ('action', 'num', 'cache', 'page ', 'pagesize', 'urlrule', 'Return ', 'start', 'setpage'); $ tools = array ('json', 'xml', 'block ', 'get'); $ datas = array (); $ tag_id = md5 (stripslashes ($ html); // visualized condition $ str_datas = 'Op = '. $ op. '& tag_md5 = '. $ tag_id; foreach ($ matches as $ v) {$ str_datas. = $ str_datas? "& $ V [1] =". ($ op = 'block' & strpos ($ v [2], '$') = 0? $ V [2]: urlencode ($ v [2]): "$ v [1] = ". (strpos ($ v [2], '$') = 0? $ V [2]: urlencode ($ v [2]); if (in_array ($ v [1], $ arr )) {$ v [1] = $ v [2]; continue;} $ datas [$ v [1] = $ v [2] ;}$ str = ''; $ setpages = isset ($ setpages) & intval ($ setpages )? Intval ($ setpages): 10; $ num = isset ($ num) & intval ($ num )? Intval ($ num): 20; $ cache = isset ($ cache) & intval ($ cache )? Intval ($ cache): 0; $ return = isset ($ return) & trim ($ return )? Trim ($ return): 'data'; if (! Isset ($ urlrule) $ urlrule = ''; if (! Empty ($ cache )&&! Isset ($ page) {$ str. = '$ tag_cache_name = md5 (implode (\'&\','. self: arr_to_html ($ datas ). '). \''. $ tag_id. '\'); if (! $ '. $ Return. '= tpl_cache ($ tag_cache_name ,'. $ cache. ') {';} if (in_array ($ op, $ tools) {switch ($ op) {case 'json ': if (isset ($ datas ['URL']) &! Empty ($ datas ['URL']) {$ str. = '$ json = @ file_get_contents (\''. $ datas ['URL']. '\'); '; $ str. = '$ '. $ return. '= json_decode ($ json, true);';} break; case 'block': $ str. = '$ block_tag = pc_base: load_app_class (\ 'block _ tag \', \ 'block \ ');'; $ str. = 'echo $ block_tag-> pc_tag ('. self: arr_to_html ($ datas ). ');'; break;} else {if (! Isset ($ action) | empty ($ action) return false; if (file_exists (APPPATH. 'libraries '. DIRECTORY_SEPARATOR. $ op. '_ tag. php ') {$ str. = 'If (! Isset ($ CI) $ CI = & get_instance (); $ CI-> load-> library ("'. $ op. '_ tag "); if (method_exists ($ CI-> '. $ op. '_ tag ,\''. $ action. '\') {'; if (isset ($ start) & intval ($ start) {$ datas ['limit'] = intval ($ start ). ','. $ num;} else {$ datas ['limit'] = $ num;} if (isset ($ page) {$ str. = '$ pagesize = '. $ num. '; $ str. = '$ page = intval ('. $ page. ')? Intval ('. $ page. '): 1; if ($ page <= 0) {$ page = 1 ;}'; $ str. = '$ offset = ($ page-1) * $ pagesize; $ urlrule = "'. $ urlrule. '";'; $ datas ['limit'] = '$ offset. ",". $ pagesize '; $ datas ['action'] = $ action; $ str. = '$ '. $ op. '_ total = $ CI-> '. $ op. '_ tag-> count ('. self: arr_to_html ($ datas ). ');'; $ str. = 'If ($ '. $ op. '_ total> $ pagesize) {$ pages = pages ($ '. $ op. '_ total, $ page, $ pagesize, $ urlrule);} else {$ pages = "" ;}';} $ st R. = '$ '. $ return. '= $ CI-> '. $ op. '_ tag-> '. $ action. '('. self: arr_to_html ($ datas ). ');'; $ str. = '}' ;}} if (! Empty ($ cache )&&! Isset ($ page) {$ str. = 'If (! Empty ($ '. $ return. ') {setcache ($ tag_cache_name, $ '. $ return. ', \ 'tpl _ data \');} '; $ str. = '}';} return "<". "? Php ". $ str ."? "."> ";}/*** PC tag end */static private function end_pc_tag () {return'
 \ ';}?> ';}/*** Convert data to HTML code * @ param array $ data array */private static function arr_to_html ($ data) {if (is_array ($ data )) {$ str = 'Array ('; foreach ($ data as $ key => $ val) {if (is_array ($ val) {$ str. = "'$ key' => ". self: arr_to_html ($ val ). ",";} else {if (strpos ($ val, '$') == 0) {$ str. = "'$ key' => $ val,";} else {$ str. = "'$ key' => '". self: new_addslashes ($ val ). "'," ;}} return $ str. ')';} return false; }/*** Return the strings or arrays processed by addslashes * @ param $ string or strings to be processed * @ return mixed */function new_addslashes ($ string) {if (! Is_array ($ string) return addslashes ($ string); foreach ($ string as $ key => $ val) $ string [$ key] = new_addslashes ($ val ); return $ string ;}}

Then add a template function in global_helper.

If (! Function_exists ('Template ')) {/*** template call ** @ param $ module * @ param $ template * @ param $ istag * @ return unknown_type */function template ($ module = 'expatree ', $ template = 'index', $ style = 'expatree ', $ return_full_path = true) {global $ CI; if (! Isset ($ CI) $ CI = & get_instance (); if (! $ Style) $ style = 'default'; $ CI-> load-> library ('Template _ cache', 'Template _ cache '); $ template_cache = $ CI-> template_cache; // address for generating the compilation template $ compiledtplfile = $ template_cache-> cache_path.DIRECTORY_SEPARATOR. 'caches _ template '. DIRECTORY_SEPARATOR. $ style. DIRECTORY_SEPARATOR. $ module. DIRECTORY_SEPARATOR. $ template. EXT; // view file $ tplfile = APPPATH. 'view '. DIRECTORY_SEPARATOR. $ module. DIRECTORY_SEPARATOR. $ template. EX T; if (file_exists ($ tplfile) {if (! File_exists ($ compiledtplfile) | (@ filemtime ($ tplfile)> @ filemtime ($ compiledtplfile) {$ template_cache-> template_compile ($ module, $ template, $ style) ;}} else {// If not, retrieve the default style template $ compiledtplfile = $ template_cache-> cache_path.DIRECTORY_SEPARATOR. 'caches _ template '. DIRECTORY_SEPARATOR. 'default '. DIRECTORY_SEPARATOR. $ module. DIRECTORY_SEPARATOR. $ template. EXT; if (! File_exists ($ compiledtplfile) | (file_exists ($ tplfile) & filemtime ($ tplfile)> filemtime ($ compiledtplfile) {$ template_cache-> template_compile ($ module, $ template, 'default');} elseif (! File_exists ($ tplfile) {show_error ($ tplfile, 500, 'Template does not exist (0) ') ;}} if ($ return_full_path) return $ compiledtplfile; else return 'caches _ template '. DIRECTORY_SEPARATOR. $ style. DIRECTORY_SEPARATOR. $ module. DIRECTORY_SEPARATOR. $ template ;}}

Then add a method in MY_Controller.php.

/*** Automatic template call ** @ param $ module * @ param $ template * @ param $ istag * @ return unknown_type */protected function view ($ view_file, $ page_data = false, $ cache = false) {$ view_file = $ this-> template ($ this-> page_data ['Controller _ name']. $ this-> page_data ['Module _ name'], $ view_file); $ this-> load-> view ($ view_file, $ page_data );}

In this way, you can directly use the phpcms template syntax.

Http://www.bkjia.com/PHPjc/752345.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/752345.htmlTechArticleCodeIgniter is very suitable for small site application development, but its built-in view function may not understand PHP front-end staff to bring trouble. In contrast, the view template parsing of phpcms is much more powerful...

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.