Implementation method of generating static pages based on PHP

  • 2020-06-03 05:59:03
  • OfStack


//  methods 1 Generate static pages from the template 
// replaceTemplateString The replace () function is used to replace a string specified in a template 
function replaceTemplateString($templateString) {
    //  The variable to replace 
    $title = " The article title ";
    $body = " Here is the body of the article ";
    //  Replace the string specified in the template 
    $showString = str_replace ( "%title%", $title, $templateString );
    $showString = str_replace ( "%body%", $body, $showString );
    //  Returns the result of the replacement 
    return $showString;

$template_file = "template.html";
$new_file      = "new.html";
//  Template file pointer 
$template_juBing = fopen ( $template_file, "r" );
//  A pointer to the file to be generated 
$newFile_juBing = fopen ( $new_file, "w" );
//  way 1 Gets the overall template content string, replaces it, and assigns it to a new file 
$templateString = fread ( $template_juBing, filesize ( $template_file ) );
$showString = replaceTemplateString ( $templateString ); //  Replace the string in the template 
fwrite ( $newFile_juBing, $showString ); //  Writes the replacement to the generated HTML file 
//  way 2 Each line of the template content string is cyclically read, replaced and added to the new file in turn 
while ( ! feof ( $template_juBing ) ) { // feof()  The function checks whether the end of the file has been reached. Returns if the file pointer reaches the end or an error occurs  TRUE . Otherwise returns FALSE (including  socket  Timeouts and other cases). 
    $templateString = fgets ( $template_juBing ); // fgets(file,length)  Reads from the file pointer 1 Row and returns a length of at most  length - 1  A byte long string, including a newline character. If not specified  length , the default is  1K Or,  1024  Bytes. 
    $showString = replaceTemplateString ( $templateString );
    fwrite ( $newFile_juBing, $showString ); //  The first 1 When writing to an open pointer file, it replaces the contents of the pointer file. Before the pointer is closed, fwrite A function adds content after it has been added 
//  Close file pointer 
fclose ( $newFile_juBing );
fclose ( $template_juBing );
 The relationship between a database and static pages 
 Typically added to the database 1 After the same message, generate 1 A static page of this information, so it's best to add it to a database table 1 Field stores the path filename of the corresponding static page for easy modification and deletion in the future 
 Template replacement 
1 In general, if you need to modify static HTML The template of the page, the usual practice is to put all that has been generated HTML The page is deleted, and a new one is created HTML Page. ( Or you could say all of them are re-overwritten )
 Dynamic operations on static pages 
 Sometimes in the creation of static HTML The top of the page also needs to be done 1 Some dynamic operations. For example, each story in the news system counts clicks. 
 through 1 The width and height are both 0 Pixel image control to hide the call 1 a php Page to achieve the page counter function, such as 
<img width='0' height='0' src='counter.php?fileid=S001'>
 Static pages of linked directories 
 Typically for systems that use static pages, the directory page of the linked list is also generated statically HTML The file is for visitors to view 
 Notice because every increase or decrease 1 The link list is affected by the column database information, so the static page of the link directory needs to be updated every time the database information is added and deleted. 
 Paging can be done by creating static pages of multiple linked directories. 

//  methods 2 Based on buffer generation 
ob_start (); //  When the buffer is active, and there is ob_end_clean() All output printed non-header information is not printed to the page, but stored in the internal buffer. If there is no ob_end_clean() , the information is stored in the internal buffer, and the output is printed 
this is test Output Control
echo "<br>this is test Output Control<br>";
include_once 'cache/newFile.php';
$contents = ob_get_contents (); //  Gets the information stored up to this point in the buffer, which holds only what will print to the page browser, php Execution code and so on will not be saved 
// $contents = ob_get_clean(); //  Gets the information that the buffer stores up to this point, and closes the purge buffer 

// ob_end_flush();// Output the information stored in the print buffer up to this point, and close the clear buffer 
ob_end_clean (); //  Closes the contents of the clear buffer 
file_put_contents ( $new_file, $contents );//  Writes to a file 


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

Related articles: