Caterpillar teaches you to write a template engine that belongs to you _php tutorial

Source: Internet
Author: User

#phpchina首发 #

Smarty has been seen as superfluous, and I think it is superfluous to think that smarty is superfluous .... Don't say that. Today I teach you to write a template engine, so that everyone can write a template engine belonging to their own, and after reading this article, your understanding of smarty will be further. My template engine is called stupid ("fool" means), I do not like too clever things!
The stupid template engine consists of 3 files, each of them: stupid.class.php,stupid_parser.class.php,stupid_debugger.class.php.
Stupid.class.php's task is to set variables, template paths, and display functions, and stupid_parser.class.php is the compilation of template files, stupid_debugger.class.php is used for debugging purposes.

Well, let's write stupid.class.php right now.
1. Create a new PHP file named: stupid.class.php.
Our class is called stupid, let's first design the member variable.
Member variables are: $_tpl_vars, $_tpl_file, $_parser, $_debugger;
$_tpl_vars: Used to save template variables;
$_tpl_file: Used to save the template file name;
$_parser: To save Stupidparser object is to compile the object;
$_debugger: To save Stupiddebug object, is to debug the object;

The following defines two constants that are used to store template folders and compile folders:
Define (Tpl_dir,./templates/);
Define (Tpl_c_dir,./templates_c/);

Start Coding >>>

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 bar >>>

Public Function Stupid () {
if (!is_dir (tpl_dir) | |!is_dir (TPL_C_DIR)) {
Exit (Error: Please set the template folder and the compilation folder correctly);
}
}

In the constructor, we determine whether the template path and the compilation path are set correctly.

Design our Approach
We have the following main methods in this class:
Assign (), Set_tpl_dir (), Set_parsed_dir (), display (), debug ().
Assign () Method:
The use of assign () is to set the template variable. 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);
}
}
Let's start by judging if the user has set the variable name, judging by Isset ($var) && trim ($var)! =, trim ($var)! = To prevent the user from setting the variable name with a space. If the set variable is correct, we'll save it to the member variable _tpl_ The VARs.

Display () method
The display () method is the most important method in the stupid class, he is used to display and detect whether the template is updated, updated and then compiled, no update with the original compiled files.

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!file_exists ($parsed _file) | | Filemtime ($parsed _file) < Filemtime ($template _file) This statement to determine whether the compiled and template files have been updated, have not compiled and updated template files to be recompiled. We're introducing stupid_. parser.class.php, and create the Stupidparser object to compile the template file. After compiling, we introduce the compiled file. This compiled template file is a normal php file.

Debug () method
Debugg () method is relatively simple, is to introduce stupid_debugger.class.php file, create Stupiddebuger object, call the Stupiddebuger start method to debug.

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'm going to introduce the Stupidparser class. Please continue to support. Any comments or suggestions can be made!

Show Show full phase:

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 the 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);
}
}
}
?>

http://www.bkjia.com/PHPjc/486612.html www.bkjia.com true http://www.bkjia.com/PHPjc/486612.html techarticle #phpchina首发 # Smarty has been seen as superfluous, and I think it is superfluous to think that smarty is superfluous .... Don't say that. Today I will teach you to write a template engine, let everyone ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.