Simple php template engine technology implementation, php template engine

Source: Internet
Author: User
Tags php template smarty template

Simple php template engine technology implementation, php template engine

After using smarty and tp, I want to know how the template technology is implemented. So I wrote a simple template class, it is generally to read the template file-> Replace the content of the template file-> Save or static

Main analysis of tpl. class. php

Assign Method implementation

/*** Template assignment operation ** @ param mixed $ tpl_var if it is a string, it is used as an array index. If it is an array, cyclically assigned a value * @ param mixed $ tpl_value when $ tpl_var is string. The default value is null */public function assign ($ tpl_var, $ tpl_value = null) {if (is_array ($ tpl_var) & count ($ tpl_var)> 0) {foreach ($ tpl_var as $ k => $ v) {$ this-> tpl_vars [$ k] = $ v ;}} elseif ($ tpl_var) {$ this-> tpl_vars [$ tpl_var] = $ tpl_value ;}}

Fetch method implementation

/*** Generate the compilation file * @ param string $ tplFile template path * @ param string $ comFile compilation path * @ return string */private function fetch ($ tplFile, $ comFile) {// determine whether the compilation file needs to be regenerated (whether the compilation file exists or the template file modification time is later than the modification time of the compilation file) if (! File_exists ($ comFile) | filemtime ($ tplFile)> filemtime ($ comFile) {// compile. You can also use ob_start () here () static $ content = $ this-> tplReplace (file_get_contents ($ tplFile); file_put_contents ($ comFile, $ content );}}

Simple compilation method: regular replacement according to rules

/*** Compile the file * @ param string $ content the content to be compiled * @ return string */private function tplReplace ($ content) {// escape the regular expression character $ left = preg_quote ($ this-> left_delimiter, '/'); $ right = preg_quote ($ this-> right_delimiter, '/'); // simulate the compilation variable $ pattern = array (// For example, {$ test }'/'. $ left. '\ $ ([a-zA-Z _ \ x7f-\ xff] [a-zA-Z0-9 _ \ x7f-\ xff] *)'. $ right. '/I'); $ replace = array ('<? Php echo $ this-> tpl_vars [\ '$ {1} \'];?> '); // Return preg_replace ($ pattern, $ replace, $ content );}

Display = fetch + echo

/*** Output content * @ param string $ fileName template file name */public function display ($ fileName) {// template path $ tplFile = $ this-> template_dir. '/'. $ fileName; // determine whether the template exists if (! File_exists ($ tplFile) {$ this-> errorMessage = 'template file does not exist'; return false;} // compiled file $ comFile = $ this-> compile_dir. '/'. md5 ($ fileName ). '. php'; $ this-> fetch ($ tplFile, $ comFile); include $ comFile ;}

Other attributes

// Template file storage location private $ template_dir = 'templates'; // compilation file storage location private $ compile_dir = 'compiles'; // left delimiter private $ left_delimiter = '{'; // private $ right_delimiter = '}'; // internal temporary variable, which stores the user's private $ tpl_vars = array (); // error message private $ errorMessage = ''; /*** modify the value of the Class Attribute * @ param array $ configs the relevant attribute and value to be modified * @ return bool */public function setConfigs (array $ configs) {if (count ($ configs)> 0) {foreach ($ configs as $ k => $ v) {if (isset ($ this-> $ k )) $ this-> $ k = $ v;} return true;} return false ;}

Test

Template File testTpl.html

<! DOCTYPE html> 

Output: waited: 100: this is a demo

Generate the compilation file: 972fa4d270e295005c36c1dbc7e6a56c. php

The above is all the content of this article, hoping to help you learn.

Articles you may be interested in:
  • PHP template engine SMARTY
  • [PHP] Deep introduction to template engine Smarty
  • Comparison and Analysis of many top-level php template Engines
  • Share the development experience of the MVC template engine in PHP
  • PHP native template engine simplest template engine
  • How to generate random numbers using the PHP template engine smarty and the math Function
  • ThinkPHP template engine-detailed description of how to import resource files
  • ThinkPHP method for using the smarty template engine

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.