CodeIgniter is ideal for small-site application development, but its own view features may cause problems for front-end people who do not understand PHP. In contrast phpcms view template parsing is much more powerful, so here the Phpcms template parsing function stripped out, add to Phpcms.
First, add template_cache.php to the CodeIgniter libraries
Copy CodeThe code is as follows:
/**
* Template Parsing cache
*/
Final class Template_cache {
Public $cache _path;
Public Function __construct ()
{
$CI =& get_instance ();
$this->cache_path = APPPATH. ' Views ';
}
/**
* Compile Template
*
* @param $module Module name
* @param $template Template file name
* @param $istag is a label template
* @return Unknown
*/
Public Function Template_compile ($module, $template, $style = ' default ') {
$tplfile = APPPATH. ' Views '. Directory_separator. $module. Directory_separator. $template. PHP ';
if (! file_exists ($tplfile)) {
Show_error ($tplfile, $, ' 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 write file name after $compiledtplfile compilation is complete
* @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 templates
*
* @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 ("/\{\/for\}/", " ", $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+) \s+ (\s+) \}/", " ", $STR);
$str = Preg_replace ("/\{loop\s+ (\s+) \s+ (\s+) \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::p c_tag (' $ ', ' $ ', ' $ ')", $str);
$str = Preg_replace ("/\{\/pc\}/ie", "Self::end_pc_tag ()", $str);
$str = " " . $STR;
return $str;
}
/**
* Escape//For/
*
* @param $var escaped characters
* @return the character after escaping
*/
Public Function Addquote ($var) {
Return Str_replace ("\\\", "\" ", Preg_replace ("/\[([a-za-z0-9_\-\.\x7f-\xff]+) \]/s "," [' \\1 '] ", $var));
}
/**
* Analyze PC Label
* @param string $op mode of operation
* @param string $data parameter
* @param string $html All of the HTML code to match
*/
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 ', ' setpages ');
$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 End
*/
Static Private Function End_pc_tag () {
Return ' ';
}
/**
* Convert data to HTML code
* @param array $data arrays
*/
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 that has been processed by addslashes
* @param $string 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 to the Global_helper
Copy CodeThe code is as follows:
if (! function_exists (' template '))
{
/**
* Template Invocation
*
* @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 to generate the address
$compiledtplfile = $template _cache->cache_path. Directory_separator. ' Caches_template '. Directory_separator. $style. Directory_separator. $module. Directory_separator. $template. EXT;
View File
$tplfile = APPPATH. ' Views '. 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, adjust 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, $, ' 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 in my_controller.php, add a method
Copy the Code code as follows:
/**
* Automatic template invocation
*
* @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);
}
This is basically done, you can directly phpcms the template syntax.
http://www.bkjia.com/PHPjc/825114.html www.bkjia.com true http://www.bkjia.com/PHPjc/825114.html techarticle CodeIgniter is ideal for small-site application development, but its own view features may cause problems for front-end people who do not understand PHP. Compared to the Phpcms view template parsing is much more powerful, ...