search
HomeBackend DevelopmentPHP TutorialA simple template engine class. This type of research is not perfect. I hope friends can participate in the study and research.

This is the first time I post code here. This code is mainly for PHP template engine technology research. Currently there is only a compiled version. I hope you can provide more opinions and optimization tips.
It consists of three files. I don’t know how to format it as a file, so I can only copy it. Sorry!
index.php is a configuration file, everyone will understand after taking a look
index.html Some usage examples
Templates.class.php base class
A perfect version of caching will be released later, but I hope it will not be cached. With the guidance of friends or experts, this template engine only needs to handle compilation and caching, and other considerations will not be considered for the time being. , of course, the regular replacement mode also needs to add f, w and so on. . .
I hope some friends can study my Q:
76376931

Copy_3_of_Templates.class.php file has added a cache method. Refreshing the page again will not generate a cache. It has not been considered whether some pages in the project need to be cached. This class will be added gradually in the future. I hope some friends can join me. comminicate!
  1. header('Content-Type:text/html;charset=utf-8');
  2. define('ROOT_HOST',dirname(__FILE__));
  3. define('HTML_DIR',ROOT_HOST. '/moban/');
  4. define('COMPILED_DIR',ROOT_HOST.'/data/compiled/');
  5. define('CACHE_DIR',ROOT_HOST.'/data/cache/');
  6. //Whether to enable the buffer
  7. define('NEW_CACHE', false);
  8. //Determine whether to open the buffer
  9. NEW_CACHE? ob_start(): null;
  10. //Introduce the template class
  11. require ROOT_HOST.'/lib/Templates.class.php';
  12. $_moban = new Templates();
  13. $array = array(a=>'Hello',b=>'I'm not very good, but I miss you very much',c=>'You are all here You're at home, why do you still miss me?');
  14. $xcvu = 'Hello, this is an XCVU';
  15. $zmq = "hi";
  16. $title = "This is a template engine customization method!" ;
  17. $ling = "Because a "function" is being modified????????????????";
  18. $_moban->assign('ling', $ling);
  19. $ _moban->assign('title',$title);
  20. $_moban->assign('zmq', $zmq);
  21. $_moban->assign('xcvu', $xcvu);
  22. $_moban- >assign('abc',5>4);
  23. $_moban->assign('array', $array);
  24. $_moban->display('index.html');
  25. ?>
Copy code
  1. <!-- $title -->< ;/title><li><style type="text/css"><li>*{ margin:0; padding:0;}<li>body{ font-size:12px; color:#fff; background:#999;}<li> .index { margin:0 auto; width:960px; background:#fff;height:1000px; line-height:50px; padding:20px;color:#000;}<li>.index a{color:#000; text-decoration :none;}<li>.index a:hover{ color:#F0F;}<li></style></li> <li> <li> <li><div class="index"> <li> <span style="color:#000;">BBBasd</span><span style="color:#000;">I don’t know what to say, but I want to say something</span> </li> <li> <a href="#"><!-- $ling --></a> </li> <li> <br> </li> <li> <!-- $xcvu --> </li> <li> <br> </li> <li> <!-- if $abc --> </li> <li> <p>No.1</p> </li> <li> <!-- else --> </li> <li> <p>No.2 </p> </li> <li> <!-- /if --> </li> <li> <br> </li> <li> <!-- loop $array(k,v) --> </li> <li> <!-- @k -->........<!-- @v --><br> </li> <li> <!-- /loop --> </li> <li> <!-- #This is PHP comments --> </li> <li> </div></li> <li> <li>
Copy code
  1. /* about:Richard.z
  2. * site:http://www.zmq.cc
  3. * E_mail:code@zmq.cc
  4. * date:2013/01/02/17:30
  5. * */
  6. class Templates{
  7. private $_CaChe;
  8. private $_Compiled;
  9. private $_HtmlFile;
  10. private $_FileVar;
  11. private $_KeyArr = array();
  12. public function __construct(){
  13. if(!is_dir(HTML_DIR) || !is_dir(COMPILED_DIR) || !is_dir(CACHE_DIR)){
  14. exit('Your directory does not exist!');
  15. }
  16. }
  17. public function assign($_var, $_value){
  18. if(isset($_var) && !empty($_var)){
  19. $this->_KeyArr[$_var] = $_value;
  20. }else{
  21. exit('Please set your value!');
  22. }
  23. }
  24. public function display($_File){
  25. //设置模板的变量
  26. $this->_HtmlFile = HTML_DIR.$_File;
  27. //设置编译
  28. $this->_Compiled = COMPILED_DIR.md5($_File).$_File.'.php';
  29. //设置缓存
  30. $this->_CaChe = CACHE_DIR.md5($_File).$_File.'.html';
  31. //判断模板是否存在
  32. if(!file_exists($this->_HtmlFile)){
  33. exit('Template file does not exist');
  34. }
  35. //赋值和判断读取
  36. if(!$this->_FileVar = file_get_contents($this->_HtmlFile)){
  37. exit('The template file read error!');
  38. }
  39. //if edit Compiled File date if(!file_exists($this->_Compiled) || filemtime($this->_Compiled) _HtmlFile)){
  40. $this->Set_Comilled();
  41. }
  42. //Include Compiled
  43. include $this->_Compiled;
  44. }
  45. //public function
  46. public function Set_Comilled(){
  47. $this->SetArr();
  48. $this->SetInclude();
  49. if(!file_put_contents($this->_Compiled, $this->_FileVar)){
  50. exit('Compiled files generated error!');
  51. }
  52. }
  53. //arr
  54. private function SetArr(){
  55. $_preaa = array(
  56. '//',
  57. '//',
  58. '//',
  59. '//',
  60. '//',
  61. '//',
  62. '//',
  63. '//');
  64. $_prebb = array(
  65. '_KeyArr["$1"];?>',
  66. '_KeyArr["$1"]) {?>',
  67. '',
  68. '',
  69. '_KeyArr["$1"] as $$2=>$$3) { ?>',
  70. '',
  71. '',
  72. '');
  73. $this->_FileVar = preg_replace($_preaa, $_prebb, $this->_FileVar);
  74. if(preg_match($_preaa[0], $this->_FileVar)){
  75. $this->_FileVar = $this->SetArr($this->_FileVar);
  76. }
  77. }
  78. //Include
  79. private function SetInclude(){
  80. $_preFile = '//';
  81. if(preg_match($_preFile, $this->_FileVar,$_File)){
  82. if(!file_exists($_File[1]) || empty($_File)){
  83. exit('You of Include File Error!');
  84. }
  85. $this->_FileVar = preg_replace($_preFile, "", $this->_FileVar);
  86. }
  87. }
  88. }
  89. ?>
复制代码
  1. /* about:Richard.z
  2. * site:http://www.zmq.cc
  3. * E_mail:code@zmq.cc
  4. * date:2013/01/02/17:30 || 2013/01/14/21:35
  5. * */
  6. class Templates{
  7. private $_CaChe;
  8. private $_Compiled;
  9. private $_HtmlFile;
  10. private $_FileVar;
  11. private $_KeyArr = array();
  12. public function __construct(){
  13. if(!is_dir(HTML_DIR) || !is_dir(COMPILED_DIR) || !is_dir(CACHE_DIR)){
  14. exit('Your directory does not exist!');
  15. }
  16. }
  17. public function assign($_var, $_value){
  18. if(isset($_var) && !empty($_var)){
  19. $this->_KeyArr[$_var] = $_value;
  20. }else{
  21. exit('Please set your value!');
  22. }
  23. }
  24. public function display($_File){
  25. //设置模板的变量
  26. $this->_HtmlFile = HTML_DIR.$_File;
  27. //设置编译
  28. $this->_Compiled = COMPILED_DIR.md5($_File).$_File.'.php';
  29. //设置缓存
  30. $this->_CaChe = CACHE_DIR.md5($_File).$_File.'.html';
  31. //判断模板是否存在
  32. if(!file_exists($this->_HtmlFile)){
  33. exit('Template file does not exist');
  34. }
  35. //赋值和判断读取
  36. if(!$this->_FileVar = file_get_contents($this->_HtmlFile)){
  37. exit('The template file read error!');
  38. }
  39. //if edit Compiled File date if(!file_exists($this->_Compiled) || filemtime($this->_Compiled) _HtmlFile)){
  40. $this->Set_Comilled();
  41. }
  42. //Include Compiled
  43. include $this->_Compiled;
  44. $this->SetCaChe();
  45. }
  46. //The setting cache file if you want to be generated again
  47. private function SetCaChe(){
  48. if(!file_exists($this->_CaChe) || filemtime($this->_CaChe) _Compiled)){
  49. if(NEW_CACHE){
  50. file_put_contents($this->_CaChe, ob_get_contents());
  51. ob_end_clean();
  52. include $this->_CaChe;
  53. }
  54. }
  55. }
  56. //public function
  57. public function Set_Comilled(){
  58. $this->SetArr();
  59. $this->SetInclude();
  60. if(!file_put_contents($this->_Compiled, $this->_FileVar)){
  61. exit('Compiled files generated error!');
  62. }
  63. }
  64. //arr
  65. private function SetArr(){
  66. $_preaa = array(
  67. '//',
  68. '//',
  69. '//',
  70. '//',
  71. '//',
  72. '//',
  73. '//',
  74. '//');
  75. $_prebb = array(
  76. '_KeyArr["$1"];?>',
  77. '_KeyArr["$1"]) {?>',
  78. '',
  79. '',
  80. '_KeyArr["$1"] as $$2=>$$3) { ?>',
  81. '',
  82. '',
  83. '');
  84. $this->_FileVar = preg_replace($_preaa, $_prebb, $this->_FileVar);
  85. if(preg_match($_preaa[0], $this->_FileVar)){
  86. $this->_FileVar = $this->SetArr($this->_FileVar);
  87. }
  88. }
  89. //Include
  90. private function SetInclude(){
  91. $_preFile = '//';
  92. if(preg_match($_preFile, $this->_FileVar,$_File)){
  93. if(!file_exists($_File[1]) || empty($_File)){
  94. exit('You of Include File Error!');
  95. }
  96. $this->_FileVar = preg_replace($_preFile, "", $this->_FileVar);
  97. }
  98. }
  99. }
  100. ?>
复制代码


Statement
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
Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

PHP Logging: Best Practices for PHP Log AnalysisPHP Logging: Best Practices for PHP Log AnalysisMar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP.Explain the concept of late static binding in PHP.Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Customizing/Extending Frameworks: How to add custom functionality.Customizing/Extending Frameworks: How to add custom functionality.Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools