Home  >  Article  >  Backend Development  >  Analysis of two methods of php static page generation

Analysis of two methods of php static page generation

WBOY
WBOYOriginal
2016-07-25 08:59:31878browse
  1. // Method 1, generate a static page based on the template

  2. // The replaceTemplateString function is used to replace the specified string in the template
  3. function replaceTemplateString($templateString) {
  4. // Use Variables to replace
  5. $title = "Article title";
  6. $body = "Here is the body of the article";
  7. // Replace the string specified in the template
  8. $showString = str_replace ( "%title%", $title, $templateString );
  9. $showString = str_replace ( "%body%", $body, $showString );
  10. // Return the replacement result
  11. return $showString;
  12. }

  13. $template_file = " template.html";

  14. $new_file = "new.html";
  15. // Template file pointer
  16. $template_juBing = fopen ( $template_file, "r" );
  17. // File pointer to be generated
  18. $newFile_juBing = fopen ( $ new_file, "w" );

  19. // Method 1, get the overall template content string, replace it and assign it to the new file

  20. $templateString = fread ( $template_juBing, filesize ( $template_file ) ) ;
  21. $showString = replaceTemplateString ( $templateString ); // Replace the string in the template
  22. fwrite ( $newFile_juBing, $showString ); // Write the replaced content into the generated HTML file

  23. // Method 2, read each line of the template content string in a loop, replace it and add it to the new file in turn
  24. while ( ! feof ( $template_juBing ) ) { // The feof() function detects whether the end of the file has been reached. Returns TRUE if the file pointer reaches the end or an error occurs. Otherwise, return FALSE (including socket timeout and other situations).
  25. $templateString = fgets ( $template_juBing ); // fgets(file,length) reads a line from the file pointer and returns a string up to length - 1 bytes long, including newlines. If length is not specified, it defaults to 1K, or 1024 bytes.
  26. $showString = replaceTemplateString ( $templateString );
  27. fwrite ( $newFile_juBing, $showString ); // When writing content to the opened pointer file for the first time, the original content in the pointer file will be replaced. Before the file pointer is closed, If the fwrite function adds content, it will close the file pointer after the added content
  28. }
  29. */
  30. //
  31. fclose ( $newFile_juBing );
  32. fclose ( $template_juBing ); Relationship with static pages
  33. Usually, after adding a piece of information in the database, a static page of the information is generated, so it is best to add a field in the database table to store the path file name of the corresponding static page to facilitate future modifications. Delete < /p>
  34. Template replacement

  35. Generally speaking, if you need to modify the template of a static HTML page, the usual approach is to delete all the generated HTML pages and then recreate the new HTML page. (Or all re-generated)

  36. Dynamic operations on static pages

  37. Sometimes, some dynamic operations also need to be performed on the static HTML pages created. For example, the click-through rate of each news article in the news system is counted.
  38. You can use an image control with a width and height of 0 pixels to hide a php page to implement the page counter function, such as
  39. Static page of link directory

  40. Usually for systems that use static pages, static HTML files are often generated for the directory page of the link list for visitors to browse
  41. Note This is because every addition or deletion of database information will have an impact on the link list. Therefore, every time database information is added or deleted, the static page of the link directory needs to be updated.
  42. Paging design can be completed by creating multiple static pages with linked directories.
  43. */

  44. // Method 2, generated based on the buffer

  45. ob_start (); // When the buffer is activated and there is ob_end_clean(), all non-file output is printed The header information will not be printed to the page, but will be saved in the internal buffer. If there is no ob_end_clean(), the information is both stored in the internal buffer and printed
  46. ?>
Copy code

This is test Output Control

  1. echo "
    this is test Output Control
    ";

  2. include_once 'cache/newFile.php';

  3. < p>$contents = ob_get_contents (); // Get the information stored in the buffer so far. The buffer only saves the content that will be output and printed to the page browser. PHP execution code will not be saved. // $contents = ob_get_clean( ); // Get the information stored in the buffer so far and close the clear buffer

  4. // ob_end_flush();//Output the information stored in the print buffer so far and close it Clear buffer

  5. ob_end_clean (); // Turn off clearing buffer contents

  6. file_put_contents ( $new_file, $contents ); // Write to file Content

  7. ?>

Copy code
2. Template file, template.html

  1. < ;html>
  2. %title%
  3. %title%


  4. %body%
  5. < /html>
Copy code

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn