The so-called code separation is actually just a kind of thinking. Since it is a kind of thinking, it means that there is a demand for it, there is no solution, there is no solution, there is no idea. Before that, we made the PHP program page. Both HTML and PHP are written together. What problems does this cause? There are two main problems: 1. The code is too long.
The so-called code separation is actually just a kind of thinking. Since it is a kind of thinking, it means that there is a demand for it, there is no solution, there is no solution, there is no idea. Before that, we made the PHP program page. Both HTML and PHP are written together. What problems does this cause? There are two main problems: 1. The code is too long.
The so-calledCodeSeparationIn fact, it is just a way of thinking. Since it is a way of thinking, it means that there is a need, no need, no solution, no solution, and no idea.
Before that, we made the PHP program page. Both HTML and PHP are written together.
There are two main problems:
1,CodeToo long to read, a process often spans N rowsCode. You may need several parentheses to end ..
2. It is not easy to edit webpages. When we make webpages. Usually, we first use DW and other tools to design the page and then add PHP to it.Code. However, some HTML files often require PHP output. At this timeCodeIt is not easy to return to DW to edit the style.
In short, it is difficult to modify the page style, so ourCodeOnly php is so good.CodeThe number of rows is significantly reduced by htmlCodeOn the other side, tools such as dw can also be called back for re-editing.
For example, a single table page and a PHP page are separated. Why not write them together.
If it is written together. There may be a problem. For example, when a webpage is opened. The write operation will be executed immediately. At this time, the form has not been submitted. After submission. You do not need to display a single page of the table. So there is a judgment.
If ($ _ POST ){
// Process submitted data
} Else {
// Display the form
}
Use if to divide the program into two processes.
When the form is displayed. Insert a long HTML section in the middleCode. WrittenCodeIt is inconvenient to re-read.
Sometimes I cannot find the line written. At this time.CodeSeparation. It will be useful.
Let's talk about it first. Work content of HTML and PHP respectively. Understand the nature of their work. To be accurateSeparationThey. We know that the final output to the webpage is the result after PHP Execution. It is the output result. This logic. It means one thing: All HTMLCodeAre used for output. PHPCodeWhat to do. Computing and process control. So some programmers started thinking that way. Set HTMLSeparationOpen. When you need to display it. Use the include function to include HTML.
As in the previous form submission. We can write it like this
If ($ _ POST ){
} Else {
Include ("post.htm ");
}
PHPCodeAnd HTML. Display HTML as needed. This is the most primitiveCodeSeparationSolution. There is a problem here, like when we create an article page. Need to output a large number of variables in HTML
It is impossible to include an HTML file. Here, we can use the "include" feature. Include. It refersCodeTo the current location. If this file contains echo $. But in the current program. $ A is assigned a value. So. After being called in. The value of $ a is output. Therefore, we can store HTML pages as PHP pages. The echo variable is only needed.
There is such a way of writing
HTMLCodeBack to DW for editing. The echo here does not contain anyCode. Use small PHPCodeBlock. Embedded in HTML code. These smallCodeBlock. Basically only echo does not interfere with HTML.Code. At the same time. A new requirement is put forward. The contained page. Defined variables. You need to know in advance. This feature. By now, various template engines have become popular. Still unchanged. This page will be included for output .. We call it a template. After that, we only need to write pure PHP programs. Prepare these variables and assign values. Then, when the execution of our program is about to end, we need to output the page. Include this template file. It can help us output all the variables. This is the most basicCodeSeparationImplementation methods. Let's try to write a template file. As mentioned earlier. To easily edit template files in DW and other tools, I will use DW to create a template file.
Well, let's assume that this is what we expect to output. This is a loop used to read the previous message book. I enter the output variable.
CodeAs follows:Test Template
This writer enables this PHP program to be edited in DW and other visualization tools. You can also see the original webpage effect. We only need to remember. This template requires an array variable named $ list.
This is the PHP program using the template. In the middle. When reading data. I have not used the output. Instead, it is assigned to an array variable named $ list. After reading the template, use include to include the template file. The display effect is as follows:
If you want to display different webpage styles. You only need to load different template files.CodeSeparation. Requirement: You need to know the variable names in the template. What kind of Data Structure does it need.
Template makes our PHPCodeSimplified a lot. You can focus on what PHP should do. It also makes the written PHP program easier to read and modify. However, there are still some minor problems. Our PHP programs oftenCodeVery long. Sometimes there are hundreds of variables used in it. There may be a situation. This variable is used. It was modified later. I did not find it. This situation. It is called "variable name pollution ". Some variables. We just want to use it in PHP and do not want it to be displayed in the template. But some variables. The process may be complicated. I accidentally re-assigned the value.
Then, we can encapsulate such a UDF.
Function tpl ($ name ){
Include ($ name. ". php ");
}
In this way, internal and external variables do not conflict. The new question comes again. How do we send the variables we want to assign values? Several implementation methods. The first is through a certain global variable. . All predefined variables are global variables. They can all penetrate user-defined functions. However, most predefined variables have their own purposes. $ GLOBALS is the predefined variable. However, all predefined variables are arrays. The template requires an accurate variable. You can use foreach to traverse the array to read the index and value, and retrieve the index to form a new variable and assign a value to the current value. Foreach ($ GLOBALS as $ k =>v v) $ k = $ v;
Function tpl ($ name ){
Foreach ($ GLOBALS as $ k =>v v) $ k = $ v;
Include ($ name. ". php ");
}
$v) $$k=$v; include($name . ".php");}$link = mysql_connect("localhost", "root", "qsdmt0409");mysql_select_db("test");mysql_query("set names gbk");$sql = "select * from `gbook`";$rs = mysql_query($sql);$list = array();while($line = mysql_fetch_assoc($rs)) { $list[] = $line;}$GLOBALS['list'] = $list;
tpl('tpl');?>
$ GLOBALS ['LIST'] = $ list;
Here we prepare the data. Stored in global variables. Then, use the global variable in the custom function to restore the variable.
However, considering predefined variables. It is often used for other purposes. This practice is abandoned here.
We can use custom variables. Then, the user-defined function parameters are passed in.
Function tpl ($ name, $ data ){
Foreach ($ data as $ k =>$ v) $ k = $ v;
Include ($ name. ". php ");
}
$ List = array ();
While ($ line = mysql_fetch_assoc ($ rs )){
$ List [] = $ line;
}
$ Data = array ();
$ Data ['LIST'] = $ list;
Tpl ('tpl', $ data );
There is another method. The variable is stored in another custom function. Read it again when necessary.
Static variables can do this.
function set_var($name = NULL, $value = NULL) { static $data = array(); if(is_null($name)) return $data; $data[$name] = $value;}
I define a function like this. $ Data is a static variable. Static variable. Its value will not disappear. It will be retained until the PHP operation ends. Run this function repeatedly. The static variable value is not re-assigned. If the variable name is not specified. Returns an array .. Otherwise, a new index is defined for the array. And assign a value to the specified value.
$v) $$k=$v; include($name . ".php");}$link = mysql_connect("localhost", "root", "qsdmt0409");mysql_select_db("test");mysql_query("set names gbk");$sql = "select * from `gbook`";$rs = mysql_query($sql);$list = array();while($line = mysql_fetch_assoc($rs)) { $list[] = $line;}set_var('list', $list);tpl('tpl');?>
NowCodeAs shown above. Use the set_var function. Remember the variables to be displayed in another world. Then. Before reading the template. Read the variable back. And restore to common variables. Of course. It is also a way to remember with a variable. But it always occupies program variables.
Let's try it. Assign more values.
Set_var ('title', 'Here is the title ');
Tpl ('tpl ');
To save trouble, I encapsulate them into a class.
class tpl { public function set_var($name = NULL, $value = NULL) { static $data = array(); if(is_null($name)) return $data; $data[$name] = $value; } public function p( $name) { $data = $this->set_var(); foreach($data as $k=>$v) $$k=$v; include($name . ".php"); }}
$ Tpl = new tpl (); $ link = mysql_connect ("localhost", "root", "qsdmt0409"); mysql_select_db ("test "); mysql_query ("set names gbk"); $ SQL = "select * from 'gbook'"; $ rs = mysql_query ($ SQL); $ list = array (); while ($ line = mysql_fetch_assoc ($ rs) {$ list [] = $ line;} $ tpl-> set_var ('LIST', $ list ); $ tpl-> set_var ('title', 'Here is the title'); $ tpl-> p ('tpl ');
Now, the template is used like this. Prepare the data to be assigned values. Assign a value to the specified tag. The template is called and displayed only when the last method is used. Now, ifCodeIn this way. How many benefits are brought to us.
CodeIt no longer contains HTML code, which is much clearer and simpler. We can concentrate on our PHP. You only need to prepare the variables to be output. Set_var will help us set these values. Copied to another world.
It doesn't matter if you use this variable name for other purposes. Do not worry about variable name pollution. Template file, you can easily switch back to the visual editor and re-process the furnace. If your data is read from the database. This will make our PHPCode. A clear ASCII code. This makes the PHP program no longer need to care about what encoding it is, which makes the PHP program no longer need to care about what encoding it is.Code, The reason is concerned about file encoding. Because of ourCodeContains a large amount of output content. The output content is encoded. When yourCodeOnly when PHP is used, PHP programs do not need to be encoded. Use the template. This is because the output is only once executed. In the compilation process. Some special functions are not considered .. Rules cannot be output before use. For example, setcookie header, which uses the template program. No output on the whole page .. Only available at the end. And responsible for outputCode. Only one row .. Currently, most template classes are used. It belongs to the smarty template class.