Home > php教程 > PHP开发 > body text

A PHP template, mainly to reflect the idea

黄舟
Release: 2016-12-14 11:34:33
Original
1142 people have browsed it

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}","");
//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}





{block_forum}

< ;td valign="top">{=$forum['forum_name']}

{/block_forum}



{/block_cat } 理 After processing, the {block_Forum} {block_cat} tags inside are replaced with PHP loop statements for displaying all elements of array.





if($forum['forum_cat_id'] == $cat['forum_id']) {?>



}?>
< ;?=$forum['forum_name']?>






default_index.php is included in index.php, so that it can be displayed normally.

In this way, the HTML template file can be modified and beautified with dw, which should be more convenient for artists.

template.php

Code:


/***************************************************** *****************************
* 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", "", $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)!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template