First set up two folders, a temp, store the pre-compiled files, a comp, store the compiled file, the pre-compilation file using {$title} instead of the <?php echo $title;, and then compile the former into the latter, After parsing is stored in the Comp folder
Define a parsing class smarty.class.php
<?php//defines a simple Smarty class to compile the HTML file class mini{public $temp _uri= ";//Where the template file is located Public $comp _uri= ';//The location of the template after compiling public $_var=array ();//variable array public function assign ($key, $value) {$this->_ var[$key]= $value; Public function display ($temp) {//Package $uri = $this->complie ($temp); Require ($uri); }/* String $tempplate template file name return String */Public Function Complie ($temp) {//read out the contents of the template $te =$ This->temp_uri. ' /'. $temp;//Pre-compiled files $source =file_get_contents ($te); $comp = './comp/'. $temp. PHP '; Determine if this file is present if (file_exists ($comp) &&filemtime ($comp) >filemtime ($te)) {//post-compilation file exists and will be saved earlier than the pre-compilation file return $comp; } $source =str_replace (' {$ ', ' <?php echo $this->_var[\ ', $source); $source =str_replace ('} ', ' \ ');? > ', $source); Var_dump (File_put_contents ($comp, $source)); return $comp; }}?>
Pre-compilation file in temp directory 01temp.html
<!doctype html>
Control Page 01.php
<?phprequire ('./smarty.class.php '); $title = ' mall '; $content = ' This is my mall oh '; $smarty =new mini (); $smarty->temp_uri= '. /temp '; $smarty->comp_uri= './comp ';//$smarty->complie (' 01temp.html '); $smarty->assign (' title ', $title); $ Smarty->assign (' content ', $content); $smarty->display (' 01temp.html ');? >
Run the 01.php build post-compilation file in the comp directory 01temp.html.php
<!doctype html>
Achieve the right results
Edit a small Smarty class