I wanted to strike a balance between speed and ease of use (mainly referring to the convenience of art design). So I adopted the method of generating php files from html files (compile?)
I also wanted to separate the display logic and separate html Balance the code
For example, a forum homepage (index.php):
Code:
require('./template.php');
//php file generated by html
$tpl_prefix = 'default';
//Template file name
$tpl_index = 'index';
$tpl = new Template($tpl_prefix);
$cats = array(
array('forum_id'=>'1','forum_cat_id'=>'0','forum_name'=>'PHP learning'),
array('forum_id'=>'2','forum_cat_id '=>'0','forum_name'=>'MYSQL Learning')
);
$forums = array(
array('forum_id'=>'3','forum_cat_id'=>'1' ,'forum_name'=>'PHP Advanced Tutorial'),
array('forum_id'=>'4','forum_cat_id'=>'1','forum_name'=>'PHP Basic Tutorial'),
array('forum_id'=>'5','forum_cat_id'=>'2','forum_name'=>'MYSQL related information')
);
if ($cats)
{
if ( $tpl->chk_cache($tpl_index))//Check to determine whether the PHP template file needs to be regenerated.
{
$tpl->load_tpl($tpl_index);//Load the html template file.
//Replace the PHP statement
$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}?>");
//Produce PHP template files.
$tpl->write_cache($tpl_index);
}
}
//Include PHP template file.
include($tpl->parse_tpl($tpl_index));
?>
corresponding html template File (index.html):
Code:
{block_cat}
=$cat['forum_name']?> | |||||
< ;?=$forum['forum_name']?> |
/***************************************************** *****************************
* Template class (Template)
* Last modified time: 2004.4.07 This forum uses
*
*
*
************************************************ *************************************/
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 = '';
/**
* Initialization template path.
*/
function Template($root = 'default')
{
//Template prefix (style name).
$this->tpl_prefix = $root;
//Template file path.
$this- >tpl_path = './templates/' . $root . '/';
//The generated PHP file storage path.
$this->cache_path = './template_data/' .$this->tpl_prefix . '_';
return true;
}
/**
* chk_cache, check whether the "compiled" template needs to be updated. The judgment is based on: the last modification time and whether the "compiled" file exists.
*/
function chk_cache($tpl_index)
{
$tpl_file = $this->tpl_path . $tpl_index . '.html';
$ cache_file = $this->cache_path . $tpl_index . '.php';
//Determine whether it needs to be updated.
if(!file_exists($cache_file))
{
return true;
}
elseif(filemtime($tpl_file ) > filemtime($cache_file))
{
return true;
}
}
/**
* Output template file.
*/
function parse_tpl($tpl_index,$message='')
{
return $this-> ;cache_path . $tpl_index . '.php';
}
/**
* Load template file.
*/
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);
}
/**
* Replace variables and "compile" the template.
*/
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);
fwrite($fp, $this->template);
fclose($fp);
}
/**
* Replace block.
*/
function assign_block($search,$replace)
{
$this->template = str_replace($search,$replace,$this->template);
}
}
?>
Thank you For reading, if you want to get more related content, please pay attention to php Chinese website (m.sbmmt.com)!