In php, we will use three functions to generate an html page: ob_start (), ob_get_contents (), and ob_end_clean (), and then generate the obtained content to the fopen function. example ,... in php, we will use three functions to generate an html page: ob_start (), ob_get_contents (), and ob_end_clean (), and then generate the obtained content to the fopen function.
For example, the following functions are used: ob_start (), ob_get_contents (), and ob_end_clean ().
Knowledge Point:
1. the ob_start function is generally used to enable caching. Note that no output, such as spaces or characters, is allowed before the ob_start function is used.
2. the ob_get_contents function is mainly used to obtain the cached content and return it as a string. Note that this function must be called before the ob_end_clean function; otherwise, the cached content cannot be obtained.
3. the ob_end_clean function clears the content in the cache and closes the cache. if the cache is successful, True is returned. if the cache fails, False is returned.
The Output Control function uses and controls the cache to generate static HTML pages. It also uses the php file read/write function.
PHP generates static page instance code. the code is as follows:
".""."PHP static website tutorial".""."Welcome to PHP website development tutorial www.phprm.com. This article mainly introduces the static PHP website page method.".""; $ Out1 = ob_get_contents (); ob_end_clean (); $ fp = fopen (" leapsoulcn.html "," w "); if (! $ Fp) {echo "System Error"; exit () ;}else {fwrite ($ fp, $ out1); fclose ($ fp); echo "Success" ;}?>
Example: use the framework template to generate a recently popular php Framework. thinkphp is used as an example.
Thinkphp provides an effective method to generate static pages. it is described in the tp2.0 Manual. it is not described in the 3.0 manual, but the 3.0 method still exists. the code is as follows:
$ This-> buildHtml ('static file', 'static path', 'Template file ');
Parameter 1:A static file is the generated static file name. The Complete file storage path is: static path/static file. For example, if a/index is set for a static file, the saved path is the Project path/Html/a/index.html (the default static path is under the Html folder of the project path, and cannot be created by yourself)
Parameter 2:Static path. the default path is described above. in 3.0, you can add parameters to the entry file to change the static path. Define ('HTML _ path', './'); (the static PATH is the root directory of the website)
Parameter 3:Template File. I think the official description is incorrect. to be precise, it should be the target module, that is, the module that needs to generate static files. Format: module name: Operation. For example, if the method a under Index is to be generated is a static file, it is Index:. If it is null, the static file for the current operation is generated by default.
1. define the location of the file that generates the static page in the index. php file of the portal file, and create an Html folder in the directory. the code is as follows:
Define ('HTML _ path', './html'); // www.phprm.com
2. configure the static page type in config. php in the config folder of the root directory. set it to html. the code is as follows:
'HTML _ FILE_SUFFIX '=> '.html', // Default static file suffix
3. create an indexfolder in the "Tpl/default/" directory, and create an index.html template under the folder. The template contains "this is an example of generating a static page.
4. the code in the controller IndexAction. class. php is as follows:
Class IndexAction extends Action {public function index () {$ this-> buildHtml ('name', HTML_PATH. '/', 'template', 'utf8'); // Note: utf8 cannot be written as UTF-8 $ this-> display ();}
Explanation:BuildHtml is the built-in function of thinkphp. it indicates that the index template file is generated as a static webpage in utf8 encoding format. the file name is 1. html, which exists in the root directory's htm file. this function has five parameters, as shown below:
BuildHtml ($ htmlfile, $ htmlpath, $ templateFile, $ charset, $ contentType = 'text/html ')
Parameters:
$ Static file name generated by htmlfile
$ Static file path generated by htmlpath
$ TemplateFile: specifies the static template file to be generated.
$ Charset: encoding format for generating static files
$ ContentType: type of the static file generated
Php generates a static page with the following code:
Class AddhtmlAction extends CommonAction {public function index () {$ this-> assign ('ABC', 'abc222222222 '); $ pagename = '2017 www.phprm.com '; // page name $ temppath = ''; // Display template path // check whether the static template IS displayed in the configuration file if (C ('is _ HTML ')) {// determine whether a static page has been generated if (! Is_file (HTML_PATH. '/'. $ pagename. 'html') $ this-> buildHtml ($ pagename, HTML_PATH. '/', 'index', 'utf8'); // Note: index is a dynamic template. utf8 cannot be written as UTF-8 $ temppath = HTML_PATH. '/'. $ pagename. 'html' ;}$ this-> display ($ temppath );}}
Link to this article:
Add to favorites ^ please keep the tutorial address.