PHP template principle on PHP template engine smarty Template principle

Source: Internet
Author: User
Tags php template smarty template



MVC is a great idea in the development, so that the development code has a clearer level, so that the code is divided into three layers of each job, whether it is the writing of code and later reading and maintenance, has provided a great convenience.



In the development of PHP, the view layer is not allowed to have PHP code to manipulate the database and so on to  we generally in the controller layer controllers, we have the view layer to show the data ready, convenient view layer directly to display.



Smarty template technology allows data and views to be detached so that PHP code cannot appear directly in the view. In this way, the development of the previous page and the background data development, can be two-pronged, at the same time.






The use of Smarty templates is relatively simple, with two very core functions. One is Assig (), the data to be used in the template to be assigned value, one is display (), used to parse and present the final view template.



The simple code used is as follows:


include_once "Smarty.class.php"; // introducing the Smarty class $smarty New Smarty; // Create a Smarty object $smarty->assign ("name", "Zhangmiao"); // assign values for use in templates $smarty->assign ("Age", "the"); $smarty->display (' Index.tpl '); // introduce templates, show view pages


Let's look at the template source code like this:


< H1 > Test template 1</H1> My name is: {$name}<BR/ > My age is: {$age}<br/>


The browser page is like this:






Question: We do not have PHP code in the template, we only use {$name} and {$age} to display the corresponding variables, what is the ghost?



Then we look at the Smarty compiled file like this:


My name is:echo$this,var["name"];?><br/> my age is: Echo $this-var["Age"];?><br/>


So, it turned out to be a template that contained PHP code, but the template was turned into PHP code, and we gave it to the Smarty template engine to do the work.






So how does the Smarty template engine change the tags of non-PHP code inside the template into PHP code that can eventually be parsed and executed?



The main idea is: Replace.



It is divided into two steps:



1. Use the Assign function to assign a value to the tag variable to be parsed



2. Using the display function to replace the tag with the object's PHP variable



According to this idea, we have also written a simple version of the Smarty template engine, is a multi-smarty template engine design principle of an understanding. However, only the label of a single variable can be parsed, and no other labels are processed. The code is as follows:


classmysmarty{//Template Store Path  Public $template= './template/'; //post-compilation template path  Public $template _c= './template_c/'; //an array of stored variables, assigned by the Assign function  Public $var=Array(); //assigning values to variables  Public functionAssign$vkey,$value){ if($vkey!= ""){ $this-var[$vkey] =$value;//to press the data parsed in the template into an array }    } //variable substitution in templates  Public functionDisplay$path){ $template _path=$this->template.$path; $template _c_path=$this->template_c.$path.". Php; if(!file_exists($template _path)){ return false; } //re-parse the template only if the parsed template file does not exist or if the template source file has new changes if(!file_exists($template _c_path) ||Filemtime($template _path) >Filemtime($template _c_path)){ //gets the template source file that is used to replace $template _content=file_get_contents($template _path); $pattern=Array( '/\{\s*\$ ([a-za-z][a-za-z0-9_]*) \s*\}/i ' ); $replace=Array( ' <?php echo $this->var["${1}"];?> ' ); //Replace the variable symbol {$varname} in the template source file with a regular $res=Preg_replace($pattern,$replace,$template _content); //post-compilation files write to a directory file_put_contents($template _c_path,$res); } //The introduction of the compiled file, is actually executed the PHP file code include_once $template _c_path; }}


We call our own assign and display into the introduction, can also be used to parse the normal






Ext.: https://www.cnblogs.com/zmfly/p/6548761.html






PHP template principle on PHP template engine smarty Template principle


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.