<?php class Parser { private $_tpl; public function __construct($_tplFile) { if (! $this->_tpl = file_get_contents($_tplFile)) { exit('ERROR:模版文件读取错误'); } } private function parvar() { $_patten = '/<!--\s+\{$([\w]+)\}\s+-->/'; if (preg_match($_patten,$this->_tpl)) { $this->_tpl = preg_replace($_patten, "<?php echo $this->_vars[''];?>",$this->_tpl); } } private function parif(){ $_pattenif = '/<!--\s+\{if\s+$([\w]+)\}\s+-->/'; $_pattenElse = '/<!--\s+\{else\}\s+-->/'; $_pattenEndif = '/<!--\s+\{\/if\}\s+-->/'; if (preg_match($_pattenif,$this->_tpl)) { if (preg_match($_pattenEndif,$this->_tpl)) { $this->_tpl = preg_replace($_pattenif,"<?php if ($this->_vars['']){?>",$this->_tpl); $this->_tpl = preg_replace($_pattenEndif,"<?php } ?>",$this->_tpl); if (preg_match($_pattenElse,$this->_tpl)) { $this->_tpl = preg_replace($_pattenElse,"<?php }else{?>",$this->_tpl); } }else{ echo 'ERROR:IF语句没有关闭!'; } } }
After receiving the content of the template file, construct a method, obtain the content of the template file and parse it, use ordinary variables to parse the IF statement, and then parse the template file to generate a compiled file.
All resources on this site are contributed by netizens or reprinted by major download sites. Please check the integrity of the software yourself! All resources on this site are for learning reference only. Please do not use them for commercial purposes. Otherwise, you will be responsible for all consequences! If there is any infringement, please contact us to delete it. Contact information: admin@php.cn
Related Article
06 Jul 2016
What are some very useful class libraries or tool libraries for PHP?
13 Dec 2024
Linking Static Libraries to Other Static Libraries: A Comprehensive ApproachStatic libraries provide a convenient mechanism to package reusable...
06 Jul 2016
Two servers were set up using virtual machines, one as the nginx load balancing server, the other as the web server, and PHP as the background language. When I configure server_name in the nginx configuration file of the web server, when I directly access the web server IP address, I can parse the php file, but when accessing the load balancing service...
11 Nov 2024
Hidden Truths: The Leading Underscore in PHP Class MethodsWhen browsing PHP libraries, one might stumble upon class methods prefixed with a...
06 Jul 2016
I have seen many open source projects in the form of class.classname.php, but I have also seen many frameworks in the form of classname.class.php. Where should I place this class? I personally prefer the .class.php form, because in some frameworks, after importing third-party class libraries and specifying class libraries...
09 Nov 2024
Why Do Some PHP Class Methods Begin with an Underscore?While exploring PHP libraries, you might have noticed that certain developers prefer to...
Hot Tools
PHP library for dependency injection containers
PHP library for dependency injection containers
A collection of 50 excellent classic PHP algorithms
Classic PHP algorithm, learn excellent ideas and expand your thinking
Small PHP library for optimizing images
Small PHP library for optimizing images