// Method 1: generate a static page based on the template
- // ReplaceTemplateString is used to replace the specified string in the template.
- Function replaceTemplateString ($ templateString ){
- // The variable to replace
- $ Title = "article title ";
- $ Body = "this is the subject of the article ";
- // Replace the specified string in the template
- $ ShowString = str_replace ("% title %", $ title, $ templateString );
- $ ShowString = str_replace ("% body %", $ body, $ showString );
- // Return the replaced 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: obtain the overall template content string, replace the string, 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 to the generated HTML file.
/*
- // Method 2: cyclically read the content strings in each line of the template. replace the strings and add them to the new file in sequence.
- While (! Feof ($ template_juBing) {// feof () function checks whether the object has reached the end. Returns TRUE if the file pointer ends or an error occurs. Otherwise, FALSE (including socket timeout and other cases) is returned ).
- $ TemplateString = fgets ($ template_juBing); // fgets (file, length) reads a row from the file pointer and returns a string with a maximum length of length-1 bytes, including line breaks. If length is not specified, the default value is 1 K, or 1024 bytes.
- $ ShowString = replaceTemplateString ($ templateString );
- Fwrite ($ newFile_juBing, $ showString); // when you write content to the opened pointer file for the first time, the original content in the pointer file will be replaced. before the pointer is closed, the fwrite function adds content after the added content
- }
- */
- // Close the file pointer
- Fclose ($ newFile_juBing );
- Fclose ($ template_juBing );
/*
- Relationship between databases and static pages
- Generally, after a piece of information is added to the database, a static page of the information is generated. Therefore, it is best to add a field in the database table to store the path file name corresponding to the static page for later modification and deletion.
Template replacement
- Generally, if you need to modify the static HTML page template, you can delete all generated HTML pages and recreate them. (Or overwrite all to generate)
Dynamic operations on static pages
- Sometimes, some dynamic operations are also required on the created static HTML page. For example, in a news system, the click rate of each piece of news is counted.
- You can use an image control with a width and height of 0 pixels to hide and call a php page to implement the page counter function, as shown in figure
Link to the static page of the Directory
- Generally, for systems that use static pages, the directory pages of the connected list are also generated as static HTML files for visitors to browse.
- Note that each increase or decrease of a database information will affect the link list. Therefore, you must update the static page of the link directory every time you add or delete the database information.
- The paging design can be completed by creating static pages of multiple linked directories.
- */
// Method 2, generated based on the buffer
- Ob_start (); // when the buffer zone is activated and there is ob_end_clean (), all output non-file header information will not be printed to the page, but saved in the internal buffer zone. If no ob_end_clean () exists, the information is printed both by the internal buffer and output.
- ?>
-
This is test Output Control
Echo" This is test Output Control ";
- Include_once 'cache/newFile. php ';
$ Contents = ob_get_contents (); // obtain the information stored so far in the buffer. the buffer only saves the printed content that will be output to the browser on the page. php code execution will not be saved.
- // $ Contents = ob_get_clean (); // obtain the information stored so far in the buffer and disable clearing the buffer.
// Ob_end_flush (); // output the information stored so far in the buffer and disable clearing the buffer.
Ob_end_clean (); // Close the buffer clearing content
File_put_contents ($ new_file, $ contents); // write content to the file
- ?>
2. template.html
-
- % Title %
- % Title %
%body%
|