Class template { Var $ temp; // file name of the template page read (including path) Var $ html; // The html file name to be generated (including the path) Var $ err; // error code Var $ test; // data stream Var $ arr; // array mode to be replaced (key name-> Template tag, key value-> replace content) Function template () { $ This-> temp = ""; $ This-> html = ""; $ This-> err = 0; $ This-> test = ""; } Function templatehtml ($ temp, $ html, $ arr) { $ Err = $ this-> chkfile ($ temp ); If (int) $ err = 0) { $ Fp = fopen ($ temp, "r"); // open the template page in read-only mode $ Test = fread ($ fp, filesize ($ temp); // read the data stream from the template page $ Test = $ this-> arr_replace ($ arr, $ test); // replace the file $ Err = $ this-> writefile ($ html, $ test); // Generate a static page } Echo "generated by Template page". $ temp. ". $ html. $ this-> error ($ err ); Return; } /* * Determine whether a file exists * An error message is returned. */ Function chkfile ($ file) { If (file_exists ($ file )) { Return 0; } Return 1; } /* * Replace the data stream mode based on the content of the array File (key name-> Template tag, key value-> replace content) * Return data stream * Parameter $ arr: Array * Parameter $ test: Data Stream */ Function arr_replace ($ arr, $ test) { $ Ss = $ test; Foreach ($ arr as $ key => $ value) { $ Ss = str_replace ($ key, $ value, $ ss ); } Return $ ss; } /* * Write data streams to files. * Return execution status * Parameter $ html: the html file to be generated * Parameter $ test: Data Stream */ Function writefile ($ html, $ test) { $ Stat = 2; If ($ this-> chkfile ($ html) = 0) // checks whether the object exists. { $ Stat = 0; // returns 0 if it already exists. } If ($ f = fopen ($ html, "w") // open the file in writing mode. if the file does not exist, create { Fputs ($ f, $ test ); Fclose ($ f ); $ Stat = 0; // if the write is successful, 0 is returned. } Else { $ Stat = 2; // 2 is returned if a write failure occurs. } Return $ stat; } /* * Error message * An error message is returned. * Parameter $ err: error number * Parameter $ file: error file */ Function error ($ err) { $ Message = ""; Switch (int) $ err) { Case 0: $ Message = "the static page is successfully generated "; Break; Case 1: $ Message = "opening the template page failed. check whether the template exists "; Break; Case 2: $ Message = "file generation failed. check the directory permission "; Break; Default: $ Message = "Unknown error "; } Return $ message; } /* * * Mainly used to read template pages and return data streams (such as top and foot public files ,) * Parameter $ file: Template page path */ Function readhtml ($ file) { $ Test = ""; $ Err = $ this-> chkfile ($ file ); If ($ err = 0) { $ Fp = fopen ($ file, "r"); // open the template page in read-only mode $ Test = fread ($ fp, filesize ($ file); // read the data stream from the template page } Else { $ Test = $ file. $ this-> error ($ err ); } Return $ test; } /* * * It is mainly used to delete generated files and does not return * Parameter $ file: file path */ Function delete_file ($ file) { If (file_exists ($ file )) { $ Delete = chmod ($ file, 0777 ); $ Delete = unlink ($ file ); If (file_exists ($ file )) { $ Filesys = eregi_replace ("/", "", $ file ); $ Delete = system ("del $ filesys "); Clearstatcache (); If (file_exists ($ file )) { $ Delete = chmod ($ file, 0777 ); $ Delete = unlink ($ file ); $ Delete = system ("del $ filesys "); } } Clearstatcache (); } } } /* Call method Demo.html code
{Title} {Text}
$ SC = new template (); // $ SC = new template ($ tmp, $ filename, $ arr );()
$ Tmp = "../template/temp. php Tutorial"; // template page $ Filename = "test.html"; // generate a page $ Foot = "../foot.html"; // contains the base file, the header file is the same $ Arr = array (); $ Arr ["{title}"] = "New title "; $ Arr ["{text}"] = "new content "; $ Arr ["{foot}"] = $ SC-> readhtml ($ foot ); $ SC-> templatehtml ($ tmp, $ filename, $ arr ); |