#phpchina开户#
Smarty has always been regarded as a redundant thing. I think people who think Smarty is redundant are redundant.... Let’s not talk about this anymore. Today I will teach you how to write a template engine so that everyone can write a template engine of their own. After reading this article, your understanding of Smarty will be better. My template engine is called Stupid (meaning "stupid"), I don't like things that are too clever!
The Stupid template engine is composed of 3 files, they are: stupid.class.php, stupid_parser.class.php, stupid_debugger.class.php.
The task of Stupid.class.php is to set variables, template paths, and display functions, while Stupid_parser.class.php is used to compile template files, and Stupid_debugger.class.php is used for debugging.
Okay, let's write stupid.class.php now.
1. Create a new PHP file named: stupid.class.php.
Our class is called Stupid. Let’s design the member variables first.
Member variables include: $_tpl_vars, $_tpl_file, $_parser, $_debugger;
$_tpl_vars: used to save template variables;
$_tpl_file: used to save the template file name;
$_parser: The one that saves the StupidParser object is the compiled object;
$_debugger: What saves the StupidDebug object is the debugging object;
two constants are defined below, which are used to store the template folder and the compilation folder:
define('TPL_DIR', './templates/');
define('TPL_C_DIR', './templates_c/');Start
coding>>>
<?php
define('TPL_DIR', './templates/');
define('TPL_C_DIR', './templates_c/');
class Stupid {
private $_tpl_vars;
private $_tpl_file;
private $_parser;
private $_debugger;
}
?>
Start writing a constructor>>>
public function Stupid() {
if(!is_dir(TPL_DIR) || !is_dir(TPL_C_DIR)) {
exit('Error: Please set the template folder and compilation folder correctly');
}
}
In the constructor, we determine whether the template path and compilation path are set correctly.
Design our methods. Our class mainly has the following methods:
assign(), set_tpl_dir(), set_parsed_dir(), display(), debug().
assign() method:
The purpose of assign() is to set template variables. The code is as follows >>>
public function assign($var, $value) {
if(isset($var) && trim($var) != '') {
$this->_tpl_vars[$var] = $value;
return true;
} else {
exit('Error: Please set variable name');
}
}
We first determine whether the user has set the variable name, use isset($var) && trim($var) != '' to determine, trim($var) != '' prevents the user from setting the variable name with spaces. If set If the variable is correct, we will save it to the member variable _tpl_vars.
display() method
The display() method is the most important method in the Stupid class. It is used to display and detect whether the template has been updated. If it is updated, it will be compiled again. If it is not updated, the original compiled file will be used.
The code is as follows >>>
public function display( $tpl_file) {
$template_file = TPL_DIR.$tpl_file;
if(!file_exists($template_file)) {
exit('Error: template file does not exist');
}
$parsed_file = TPL_C_DIR.md5($tpl_file).'.php';
if(!file_exists($parsed_file) || filemtime($parsed_file) < filemtime($template_file)) {
require_once './stupid_parser.class.php';
$this->_parser = new StupidParser();
$this->_parser->compile($tpl_file);
}
include $parsed_file;
}
This method is based on the statement !file_exists($parsed_file)||filemtime($parsed_file)< filemtime($template_file) to determine whether it has been compiled and whether the template file has been updated. If it has not been compiled or the template file has been updated, it must be re-compiled. Compile. We will introduce stupid_parser.class.php, create a StupidParser object, and compile the template file. After compilation, we will introduce the compiled file. The compiled template file is an ordinary PHP file.
debug() method
The Debugg() method is relatively simple, which is to introduce the stupid_debugger.class.php file, create a StupidDebuger object, and call the start method of StupidDebuger for debugging.
The code is as follows >>>
public function debug ($tpl_file) {
if (include_once("stupid_debugger.class.php")) {
$this->_debugger = new StupidDebugger(TPL_DIR. $tpl_file);
$this->_debugger->start();
} else {
exit('Error: Debuger class file does not exist');
}
}
At this point, our Stupid class is finished! Next time I will introduce the writing of the StupidParser class. Please continue to support. If you have any comments or suggestions, you can put forward!
show show full phase:
<?php
define('TPL_DIR', './templates/');
define('TPL_C_DIR', './templates_c/');
class Stupid {
private $_tpl_vars;
private $_tpl_file;
private $_parser;
private $_debug;
public function Stupid() {
if(!is_dir(TPL_DIR) || !is_dir(TPL_C_DIR)) {
exit('Error: Please set the template folder and compilation folder correctly');
}
}
public function assign($var, $value) {
if(isset($var) && trim($var) != '') {
$this->_tpl_vars[$var] = $value;
return true;
} else {
exit('Error: Please set variable name');
}
}
public function display($tpl_file) {
$template_file = TPL_DIR.$tpl_file;
if(!file_exists($template_file)) {
exit('Error: template file does not exist');
}
$parsed_file = TPL_C_DIR.md5($tpl_file).'.php';
if(!file_exists($parsed_file) || filemtime($parsed_file) < filemtime($template_file)) {
require_once './stupid_parser.class.php';
$this->_parser = new StupidParser();
$this->_parser->compile($tpl_file);
}
include $parsed_file;
}
function debug($tpl_file) {
if (include_once("stupid_debugger.class.php")) {
$this->_debugger = new StupidDebugger($this->_template_dir . $tpl_file);
$this->_debugger->start();
} else {
exit('Error: Debuger class file does not exist');
}
}
}
?>