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}
{/block_cat } 理 After processing, the {block_Forum} {block_cat} tags inside are replaced with PHP loop statements for displaying all elements of array.
=$cat['forum_name']?> | |||||
< ;?=$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", "=\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 (www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.