search
HomePHP LibrariesOther librariesPHP 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

PHP template parsing classPHP template parsing class

25Jul2016

PHP template parsing class

PHP template parsing class example, php template example_PHP tutorialPHP template parsing class example, php template example_PHP tutorial

13Jul2016

PHP template parsing class instance, php template instance. PHP template parsing class examples, php template examples This article describes the PHP template parsing class. Share it with everyone for your reference. The details are as follows: phpclass template { private $vars = array(); p

PHP creation and parsing XML_PHP tutorialPHP creation and parsing XML_PHP tutorial

13Jul2016

PHP creates and parses XML. As XML continues to heat up, PHP provides a large number of standard libraries (class libraries) specifically designed to handle XML to manipulate XML. For example, DOM XML extension rewriting, SimpleXML extension, SOAP extension

24 useful PHP class libraries to share, 24php class libraries_PHP tutorial24 useful PHP class libraries to share, 24php class libraries_PHP tutorial

13Jul2016

24 useful PHP libraries to share, 24php libraries. 24 useful PHP libraries to share, 24php libraries Currently, PHP is the most popular scripting language used for web development. You can easily find a lot of information about PHP on the Internet, including documentation, tutorials,

PHP template parsing example_PHP tutorialPHP template parsing example_PHP tutorial

14Jul2016

PHP template parsing example. Below is an example of PHP template parsing. Convert {youku id_XMTc5ODgwOTA4} to the corresponding playback code through regular expressions. ?php header(Content-type:text/html; charset=utf-8); $te

15 Very Practical Open Source PHP Class Libraries, 15 Open Source Class Libraries_PHP Tutorial15 Very Practical Open Source PHP Class Libraries, 15 Open Source Class Libraries_PHP Tutorial

13Jul2016

15 very practical open source PHP libraries, 15 open source libraries. 15 very practical open source PHP class libraries, 15 open source class libraries The PHP library provides developers with a standard interface, which helps developers make full use of object-oriented programming in PHP. These libraries are specific

See all articles