Static Generation Method in php

Source: Internet
Author: User
Tags php website

Static Generation Method in php

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:  

<?
Ob_start ();

Echo ""<Head> ".
"<Title> PHP static website tutorial </title> ".
"</Head> ".
"<Body> welcome to PHP website development tutorial network www.111cn.net this article mainly introduces the static PHP website page method </body> ".
"</Html> ";

$ 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,

Using 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 (as described in the tp2.0 manual, 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: static file refers to 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. You can add parameters to the entry file in 3.0 to change the static path. Define ('html _ path', './'); (the static PATH is the root directory of the website)


Parameter 3: Template file. I feel that 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 file location for generating static pages in the index. php file of the entry file, and create an Html folder in the directories.

The Code is as follows:  

Define ('html _ path', './HTML'); // www..net


2. Configure the static page type in config. php In the config folder of the root directory and 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/folder" directory and create an index.html Template under the folder. The template contains the following content: this is an example of generating a static page.

4. Code in the Controller IndexAction. class. php

The Code is as follows:  

Class IndexAction extends Action {
Public function index (){
$ This-> buildHtml ('name', HTML_PATH. '/', 'template', 'utf8'); // Note: UTF-8 cannot be written here.
$ This-> display ();
}
}

Explanation:
BuildHtml is the built-in function of thinkphp, which means:
Generate a static webpage in utf8 encoding format from the index template file. The file name is 1. html and is stored in the htm file in the root directory.
The function has five parameters:
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

// Generate a static page

The Code is as follows:  
Class AddhtmlAction extends CommonAction {
Public function index (){
$ This-> assign ('abc', 'abc222222222 ');
$ Pagename = '1970 www..net '; // Page name
$ Temppath = ''; // display the 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 UTF-8.
$ Temppath = HTML_PATH. '/'. $ pagename. 'html ';
}
$ This-> display ($ temppath );
}
}

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.