Home PHP Libraries Other libraries PHP template parsing class
PHP template parsing class
<?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.

Disclaimer

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

What are some very useful class libraries or tool libraries for PHP? What are some very useful class libraries or tool libraries for PHP?

06 Jul 2016

What are some very useful class libraries or tool libraries for PHP?

How Do I Link Static Libraries That Depend on Other Static Libraries? How Do I Link Static Libraries That Depend on Other Static Libraries?

13 Dec 2024

Linking Static Libraries to Other Static Libraries: A Comprehensive ApproachStatic libraries provide a convenient mechanism to package reusable...

Load balancing - nginx php parsing Load balancing - nginx php parsing

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

Why Do PHP Developers Use Leading Underscores in Class Methods? Why Do PHP Developers Use Leading Underscores in Class Methods?

11 Nov 2024

Hidden Truths: The Leading Underscore in PHP Class MethodsWhen browsing PHP libraries, one might stumble upon class methods prefixed with a...

laravel - What should the standardized PHP class library naming look like? laravel - What should the standardized PHP class library naming look like?

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

Why Do Some PHP Class Methods Start with an Underscore? Why Do Some PHP Class Methods Start with an Underscore?

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

See all articles