The Smarty template engine is designed to separate the PHP code from the HTML code;
Rationale: Analyze the tags in the HTML template, generate the appropriate PHP files, and then introduce the PHP
Access procedure: 1, user access. php files: Include template files in the php file, pass some necessary attributes in the template file
/**
Summary
* $smarty workflow;
* 1, the need to display the global variable assignment, plug into the object inside the property, an array
* 2, compile the template, the {$ tag}, parse into the corresponding Phpecho code
* 3, the introduction of compiled PHP files
*
*
* Steps to use Smarty
* 1, Smarty is a class, to use, you need to first introduce and instantiate
* 2, assign assigned value
* 3, display[compile to output]
*
* Smarty's argument
* 1, compile the template, waste time
* 2, to re-assign the variable to the object's properties, increase the cost
*/
1. Pages visited
demo.php
<?PHPinclude'./mini.php ';$mini=NewMini ();$mini->template_dir= './template ';$mini->compile_dir= './compile ';//echo $mini->compile (' 01.html ');$title= ' Huo ';$content= ', hello '; $mini->assign (' title ',$title);$mini->assign (' content ',$content);//Print_r ($mini->_tpl_var);//include $mini->compile ("01.html");$mini->display (' 01.html ');
2. Template engine to be introduced
<?PHPclassmini{ Public $template _dir= ";//where the template files are located Public $compile _dir= ";//the location of the template after compilation//define an array to receive external variables Public $_tpl_var=Array(); Public functionAssign$key,$value){ $this->_tpl_var[$key]=$value; } /** String $temlate template file name * return String * Process: Read the contents of the specified template and compile it into PHP **/ Public functionDisplay$template){ $comp=$this->compile ($template); include $comp; } /** String $temlate template file name * return String * Process: Read the contents of the specified template and compile it into PHP **/ Public functionCompile$template){ $temp=$this->template_dir. '/'.$template;//template file $comm=$this->compile_dir. '/'.$template.‘. PHP ';//post-compilation file//Determine if template file exists if(file_exists($comm) &&Filemtime($temp) <Filemtime($comm)){ return $comm; } //read out template content $source=file_get_contents($temp); //Replace template content $source=Str_replace(' {$ ', ' <?php echo $this->_tpl_var[\ ',$source); $source=Str_replace('} ', ' \ ');?> ',$source); //Save the compiled content as a. php file file_put_contents($comm,$source); return $comm; }}
Mini version Smarty production, template making process, method, thought