-
-
// Method 1, generate a static page based on the template - // The replaceTemplateString function is used to replace the specified string in the template
- function replaceTemplateString($templateString) {
- // Use Variables to replace
- $title = "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 );
- // Return the replacement result
- return $showString;
- }
$template_file = " template.html";
- $new_file = "new.html";
- // Template file pointer
- $template_juBing = fopen ( $template_file, "r" );
- // File pointer to be generated
- $newFile_juBing = fopen ( $ new_file, "w" );
// Method 1, get the overall template content string, replace it and assign it to the new file
- $templateString = fread ( $template_juBing, filesize ( $template_file ) ) ;
- $showString = replaceTemplateString ( $templateString ); // Replace the string in the template
- fwrite ( $newFile_juBing, $showString ); // Write the replaced content into the generated HTML file
// Method 2, read each line of the template content string in a loop, replace it and add it to the new file in turn - 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).
- $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.
- $showString = replaceTemplateString ( $templateString );
- 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
- }
- */
- //
- fclose ( $newFile_juBing );
- fclose ( $template_juBing ); Relationship with static pages
- 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>
Template replacement
- 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)
Dynamic operations on static pages
- 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.
- 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
Static page of link directory
- 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
- 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.
- Paging design can be completed by creating multiple static pages with linked directories.
- */
// Method 2, generated based on the buffer
- 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
- ?>
-
-
-
-
Copy code
This is test Output Control
-
-
echo " this is test Output Control ";
- include_once 'cache/newFile.php';
- < 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
// ob_end_flush();//Output the information stored in the print buffer so far and close it Clear buffer
ob_end_clean (); // Turn off clearing buffer contents
file_put_contents ( $new_file, $contents ); // Write to file Content
- ?>
-
-
Copy code
2. Template file, template.html
- < ;html>
- %title%
-
%title%
%body%
- < /html>
-
Copy code
|