Add the phpcms template mechanism to CodeIgniter

Source: Internet
Author: User
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, so the template parsing function of PHPCMS is removed here and added to PHPCMS.

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, so the template parsing function of PHPCMS is removed here and added to PHPCMS.

First, add template_cache.php to CodeIgniter libraries.

/**
* Template resolution cache
*/
Final class template_cache {

Public $ cache_path;
Public function _ construct ()
{
// $ CI = & get_instance ();
$ This-> cache_path = APPPATH. 'View ';
}

/**
* Compile the template
*
* @ Param $ module Name
* @ Param $ template file name
* @ Param $ whether istag 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 $ original file path of the tplfile Template
* @ Param $ After compiledtplfile is compiled, write the 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;
}

/**
* Parsing 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 (' \ 1;?> ') ", $ 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 ///
*
* @ Param $ var escape character
* @ Return escape character
*/
Public function addquote ($ var ){
Return str_replace ("\" "," \ "", preg_replace ("/\ [([a-zA-Z0-9 _\-\. \ x7f-\ xff] +) \]/s "," ['\ 1'] ", $ var ));
}

/**
* Resolve PC labels
* @ 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 ("/([a-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 ));
// Visualization conditions
$ 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 = "";}';
}
$ Str. = '$ '. $ 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 ends
*/
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;
}

/**
* Returns a string or array processed by addslashes.
* @ Param $ string the string or array 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;
// Compile the template generation address
$ 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. EXT;
If (file_exists ($ tplfile )){
If (! File_exists ($ compiledtplfile) | (@ filemtime ($ tplfile)> @ filemtime ($ compiledtplfile ))){
$ Template_cache-> template_compile ($ module, $ template, $ style );
}
} Else {
// If not, use 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.

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.