Home > Article > Backend Development > A PHP template, mainly to reflect the idea
代码: |
require('./template.php'); //由html生成的php文件的前缀,区别使用多种风格. $tpl_prefix = 'default'; //模板文件名 $tpl_index = 'index'; $tpl = new Template($tpl_prefix); $cats = array( array('forum_id'=>'1','forum_cat_id'=>'0','forum_name'=>'PHP学习'), array('forum_id'=>'2','forum_cat_id'=>'0','forum_name'=>'MYSQL学习') ); $forums = array( array('forum_id'=>'3','forum_cat_id'=>'1','forum_name'=>'PHP高级教程'), array('forum_id'=>'4','forum_cat_id'=>'1','forum_name'=>'PHP初级教程'), array('forum_id'=>'5','forum_cat_id'=>'2','forum_name'=>'MYSQL相关资料') ); if ($cats) { if ($tpl->chk_cache($tpl_index))//检查判断是否需要重新生产PHP模板文件. { $tpl->load_tpl($tpl_index);//加载html模板文件. //替换PHP语句 $tpl->assign_block("{block_cat}",""); $tpl->assign_block("{/block_cat}","}?>"); $tpl->assign_block("{block_forum}","\nif(\$forum['forum_cat_id'] == \$cat['forum_id']) {?>"); $tpl->assign_block("{/block_forum}","}\n}?>"); //生产PHP模板文件. $tpl->write_cache($tpl_index); } } //包含PHP模板文件. include($tpl->parse_tpl($tpl_index)); ?> |
代码: | |||||||
{block_cat}
{/block_cat} |
代码: | |||||||
}?> |
Code: |
/***************************************************** ***************************** * ’ s 's 's * --- --- to * * ************************************************ *************************************/ class Template { //$this->$template, stores template data. var $template = '' ; //Template path. var $tpl_path = ''; //Template prefix (style name). var $tpl_prefix = ''; //cache path (compiled path). var $cache_path = ''; //css file path. var $css_path = ''; //header file path. var $header_path = ''; //footer file path var $footer_path = ''; /* * * Initialize template path. */ function Template($root = 'default') { //Template prefix (style name). $this->tpl_prefix = $root; //Template file path. $this ->tpl_path = './templates/' . $root . '/'; ‐ . . '_'; return true; . $ cache_file = $this->cache_path . $tpl_index . '.php'; Determine whether updating is required. elseif(filemtime($tpl_file ) > filemtime($cache_file)) return true; return $this-> cache_path . $tpl_index . '.php'; } /** * chk_cache, check whether the "compiled" template needs to be updated. Judgment basis: last modification time, whether the "compiled" file exists. */ function load_tpl($tpl_index) { $tpl_file = $this->tpl_path . $tpl_index . '.html'; $fp = fopen($tpl_file, 'r'); $this->template = fread($fp, filesize($tpl_file)); fclose($fp); } /** * Output template file. * / function write_cache($tpl_index) { $cache_file = $this->cache_path . $tpl_index . '.php'; //Variable display. $this->template = preg_replace("/({= )(.+?)(})/is", "=\2?>", $this->template); //Interface language replacement. $this->template = preg_replace(( "/{lang +(.+?)}/ies", "$lang['main']['\1']", $this->template); $fp = fopen($cache_file, 'w '); flock($fp, 3); using using using using using using through out through through ’ ‐ ‐ ‐ ‐‐ ‐‐‐‐ and _ to be fwrite($fp, $this->template); ,$replace) { } $this->template = str_replace($search,$replace,$this->template); } } ?> |
The above introduces a PHP template, mainly to reflect the ideas, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.