The method of static generation in PHP

Source: Internet
Author: User
Tags manual php file php framework php website root directory

example, using three functions: Ob_start (), ob_get_contents (), Ob_end_clean ( )

Knowledge Points:

1, Ob_start function is generally used to open the cache, pay attention to the use of Ob_start can not have any output, such as spaces, characters and so on.

2, the Ob_get_contents function is mainly used to get the contents of the cache to return as a string, note that this function must be called before the Ob_end_clean function, otherwise the cached content is not obtained.

3, the Ob_end_clean function is mainly empty the contents of the cache and turn off the cache, success returns TRUE, Failure returns false

Output control functions, which use and control caching to generate static HTML pages, also use the PHP file read-write function.

PHP generates static page instance code

  code is as follows &nbs P;

<?  
    Ob_start ();  

    echo "     "      "<title>php website static tutorial </title>"   
    "     <body> Welcome to the PHP Web site development Tutorial Network www.111cn.net This article mainly introduces the PHP website page static method </body> ".  &NBSP
    "      
     $out 1 = ob_get_contents ();   
    Ob_end_clean ()   
     $FP = fopen ("leapsoulcn.html", "w");   
    if (! $fp)   
     {  
    echo ' System Error '; &nbSP;&NBSP
    exit ();   
   }  
    else 
    {  
    fwrite ($fp, $out 1);   
     fclose ($fp);   
    echo "Success";   
   } &NBSP;&NBSP

Example

Using framework templates to generate the most recent fire-comparing PHP framework There are thinkphp below we take it as an example.

Thinkphp provides an effective way to generate static pages, as explained in the tp2.0 manual, which is not indicated in the 3.0 manual, but the 3.0 method is still present. )

The code is as follows

$this->buildhtml (' Static file ', ' Static path ', ' template file ');


Parameter one: Static file, refers to the generated static file name, the file save path complete is: Static path/static file. For example, the static file settings A/index. Then the saved path is the project path/html/a/index.html (the default static path is not created in the Html folder of the project path)

Parameter two: Static path, which has already explained the default path, in 3.0 you can add parameters to the entry file to change the static path. Define (' Html_path ', './'); (static path is the site root directory)


Parameter three: template file, I feel the official this description is not correct, it should be the target module, is the need to generate static files module. Format: module name: operation. For example, to generate a method under index is a static file, that is index:a. If NULL, the default generates the static file for the current operation.


1. Define the file location where the static page is generated in the entry file index.php file, and then create a new HTML folder in the directory

The code is as follows

Define (' Html_path ', './html ');//www..net


2, in the root directory Config folder config.php Configure the type of static page, set to HTML

The code is as follows
' Html_file_suffix ' = > '. HTML ',//default static file suffix


3, in the "Tpl/default/" directory to create a new index folder, under the folder to create a new index.html template inside the contents of: This is to generate static page example

4, the controller indexaction. code in class. php

  code is as follows &nbs P;

   class Indexaction extends action{
  &nb sp;  Public Function Index () {
       $this-> buildhtml (' Name ', Html_path. '/', ' template ', ' UTF8 '); Note: The UTF8 here cannot be written as Utf-8
       $this-> display ();  
   }
 }

Explain:
The buildhtml here is the thinkphp built-in function, which means:
Generates a static web page in the UTF8 encoded format of the index's template file, which is named 1. html and exists in an HTM file in the root directory
The function has 5 parameters, as follows:
Buildhtml ($htmlfile, $htmlpath, $templateFile, $charset, $contentType = ' text/html ')
Parameters:
$htmlfile-generated static file name
$htmlpath-generated static file path
$TEMPLATEFILE specifies that you want to generate a static template file
$charset the encoding format for generating static files

$contentType type of static file generation

Generate a static page

  code is as follows &nbs P;

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.