Mini version Smarty production, template making process, method, thought

Source: Internet
Author: User
Tags smarty template

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.