Php has many template technologies and methods, but most of them are not convenient to use. After thinking and testing, I found a simple and clever method. My goal is to separate html from php, and html does not contain php control programs. In php4, The printHTML function is provided. For example: $ data: content; printH
Php has many template technologies and methods, but most of them are not convenient to use. After thinking and testing, I found a simple and clever method. My goal is to separate html from php, and html does not contain php control programs. In php4, the print HTML function is provided. For example: $ data = content; print H
Php has many template technologies and methods, but most of them are not convenient to use.
After thinking and testing, I found a simple and clever method.
My goal is to separate html from php, and html does not contain php control programs.
In php4, the print <HTML function is provided. Example:
$ Data = "content here ";
Print <HTML
$ Data
HTML;
?>
Output: content.
On the other hand, php has the function eval (). It can execute php code. We will combine the above
Knowledge to create a general function getTpl:
Below is the demo. php
Function gettpl($tplname,$ext;'.htm ')
{
Extract ($ GLOBALS );
$ Path = $ tplname. $ EXT;
Eval_r ("print <EOT \ n". readfn ($ path). "\ nEOT; \ n ");
}
Function readfn ($ fn)
{
$ Fp = fopen ($ fn, "rb ");
Return fread ($ fp, filesize ($ fn ));
Fclose ($ fp );
}
$ Title = 'this is the title ';
$ Data = 'content here ';
GetTpl ('Demo ');
?>
Below is demo.htm
$ Title
$ Title
$ Data
After such processing, only the variables to be replaced by php are available in demo.htm, without any additional php code.
In this way, the appearance and program logic are completely separated, providing a convenient portal for website modification and later maintenance!
Note: The above method separates php and html, representing the separation of display and logic. But at the cost of losing complex control.
Delimiters
Another method is to define the string using the delimiter syntax ("<"). An identifier should be provided after <, followed by a string, followed by the end string of the same identifier.
Warning
It is important to note that the row of the end identifier cannot contain any other character, except a semicolon. This especially means that the identifier cannot be indented, and there cannot be any spaces or tabs before or after the semicolon. It is also important to realize that the first character before the end identifier must be a line break defined in your operating system. For example, \ r is used in a Macintosh system.
If this rule is broken so that the end identifier is not "clean", it will not be regarded as the end identifier, and PHP will continue searching for it. If an end identifier cannot be found in this case, a syntax error occurs in the last line of the script.
Class Members cannot be initialized using the delimiter syntax. Use other string syntax instead. Example 11-3. Invalid example
Class foo {
Public $ bar = <
Bar
EOT;
}
?>
The delimiter text is the same as the double quotation mark string, but it does not contain double quotation marks. This means that no escape quotation marks are required in the delimiter text, but the escape Code listed above can still be used. The variable is expanded, but when the complex variable is expressed in the delimiter text, you should also note the same as the string. Example 11-4. delimiter string example
$ Str = <
Example of string
Spanning multiple lines
Using heredoc syntax.
EOD;
Class foo
{
Var $ foo;
Var $ bar;
Function foo ()
{
$ This-> foo = 'foo ';
$ This-> bar = array ('bar1', 'bar2', 'bar3 ');
}
}
$ Foo = new foo ();
$ Name = 'myname ';
Echo <
My name is "$ name". I am printing some $ foo-> foo.
Now, I am printing some {$ foo-> bar [1]}.
This shoshould print a capital 'A': \ x41
EOT;
?>
Instance:
I. Directory Planning
PhpLearn
| ---- Templates
----Index.html
| ---- Index. php
Ii. php file
Assign values to variables in index. php and include templates.
$ UserNum1 = '20140901 ';
Include ('templates/index.html '); // contains the Template
?>
Iii. template files
Index. hhtml
-----------------------
$ UserNum1// Use variables
Summary:
1. echo <
// HTML elements can exist here
2. The directory in the html template is subject to the directory (PATH) of the PHP file.
3. html templates are separated from php. You do not need to use the $ _ GET or $ _ POST method. Directly use the name as a variable
The above is a summary of the basic ideas and methods for using the heredoc template.
Only by using php templates can we truly turn ourselves from process-oriented programming to object-oriented programming.