Recommended: PHPLIB Template Handling Class (Translated Note)

xiaoxiao2021-03-06  51

/ / Contain all the template file names and the array of template names VAR $ file = array ();

// $ root is the directory VAR $ root = "" in the template file.

// $ VARKEYS [key] = "key"; // Store the key name of the text element / / $ VARVALS [key] = "value"; // Store the value of the text element VAR $ VARKEYS = array (); var $ VARVALS = array ();

// Property $ unknown Sets the processing method of unknown content var $ unknowns = "remove";

// Set whether to stop VAR $ HALT_ON_ERROR = "YES" when an error occurs;

// Record the last error message var $ last_error = ""

// Constructor Set the template file directory to the same directory // $ unknowns by default to "Remove"

Function Template ($ root = ", $ unknowns =" ​​remove ") {$ this-> set_root ($ root); $ this-> set_unknowns ($ unknowns);

/ / This method sets the value of $ root, that is, the template file storage directory function set_root ($ root) {if (! Is_dir ($ root) {$ this-> halt ("set_root: $ root is not a directory Return false;} $ this-> root = $ root; return true;}

// This method sets the value of unknowns Function Set_unknowns ($ unknowns = "keep") {$ this-> unknowns = $ unknowns;}

/ / This method is added in array file according to the key name provided by $ handle ($ handle, $ filename = ") {if (! Is_ARRAY ($ handle)) {// If $ hand is not an array IF ($ FileName == "" ") {// File name returns an error and stops $ this-> hald (" set_file: for handle $ handle filename is empty. "); return false;} // otherwise $ handle button Add a new value to the file array $ this-> file [$ handle] = $ this-> filename ($ filename);} else {// If $ hand is an array reset ($ Handle); while ($ H, $ f) = EACH ($ handle)) {// Take the key name of the $ hand as the file name of the file array, the value corresponding to the key name is the value of the value of the File array $ this-> file [$ h] = $ this- > filename ($ f);}}}

/ / This method takes out a sub-template in a parent template file, load it as a "block", and replaced with another template variable in Function set_block ($ PARENT, $ Handle, $ Name = ") {IF ( ! $ this-> loading ($ parent)) {$ this-> Halt ("Subst: Unable to Load $ Parent."); RETURN FALSE;} / / No value specified Template variable is used as a template variable Name IF ($ Name == ") $ name = $ handle; $ str = $ this-> get_var ($ pent); $ reg =" /

(. *) / n / s *

/ SM "; $ ename =" {". $ name."} "; // Extract and replace preg_match_all ($ R, $ STR, $ M); $ str = preg_replace ($ reg," {"." Name} ", $ STR); // Replace the child template in the parent template $ THIS-> set_var ($ handle, $ m [1] [0]); // Sub template content placed in array of $ handle Element $ this-> set_var ($ PARENT, $ STR);

// This method adds a new key to $ VARKEYS and $ VARVALS arrays - value to function set_var ($ VarName, $ Value = ") {if (! Is_ARRAY ($ VarName)) {// If $ varname is not an array IF ($ this-> debug) Print "Scalar: Set * $ VARNAME to * $ VALUE * / N"; $ this-> varkeys [$ varname] = "/" -> Varname ($ VarName). "/"; $ this-> varvals [$ varname] = $ value;} else {reset ($ VarName); while ($ K, $ V) = Each ($ VARNAME) ) {If ($ k)) IF ($ this-> debug) Print "Array: set * $ k * to * $ v * / n"; $ this-> varkeys [$ k] = "/" . $ this-> varname ($ k). "/"; $ this-> varvals [$ k] = $ v;}}}

/ / This method completes the change of the change in the template file to determine the content, implementing the data and the separation subsst ($ hand) {if (! $ This-> loadfile ($ hand) {// load Template file, if the failed returns an error and stops $ this-> Halt ("Subst: Unable to Load $ Handle."); Return False;} $ str = $ this-> get_var ($ hand); // Read file content To string $ STR, replace all known key values ​​and return results $ str = preg_replace ($ this-> varkeys, $ this-> varvals, $ str); return $ Str;}

// This method function is the same as Subst, just directly outputs the replacement result function PSUBST ($ Handle) {Print $ THIS-> Subst ($ handle); Return False;} // This method will represent one or more files represented by $ Handle The content is completed, stored in a Varvals array element that is $ target to key values ​​or append it, return value and method Subst the same Function Parse ($ TARGET, $ Handle, $ append = false) {if (! Is_ARRAY) $ Handle)) {$ Str = $ THIS-> Subst ($ Handle); if ($ append) {$ this-> set_var ($ TARGET, $ THIS-> Get_var ($ target). $ STR);} else { $ this-> set_var ($ TARGET, $ STR);}} else {reset ($ hand); while ($ I, $ h) = Each ($ Handle) {$ str = $ this-> Subst $ h); $ this-> set_var ($ TARGET, $ STR);}}}}}}}}}}}}}}}}}}}}}}}}}}}} Return $ Str;}

// The functionality and method of this method are the same, just the way to output Function PPARSE ($ TARGET, $ Handle, $ append = false) {Print $ THIS-> PARSE ($ TARGET, $ HANDLE, $ append); Return False;}

/ / This method returns all the arrays of all key-value pairs Composed of Function Get_vars () {reset ($ this-> varkeys); while ($ k, $ v) = Each ($ this-> VARKEYS)) {$ Result [$ k] = $ this-> varvals [$ k];} returnome

/ / This method returns the value of the corresponding key-value pair according to the key name FUNCTION GET_VAR ($ VARNAME) {= (! Is_ARRAY ($ VarName)) {// If $ varname is not an array, return directly to its corresponding VARVALS array Value Return $ this-> varvals [$ VARNAME];} else {// If the $ varname is an array, returns the array of values ​​in the Varvals array in all key values; while (List $ K, $ V) = Each ($ VARNAME)) {$ Result [$ K] = $ this-> varvals [$ k];} returnome

/ ************************************************** *************************** / FUNCTION GET_UNDEFINED ($ HANDLE) {if (! $ this-> loadfile ($ handle)) {// If the load file fails, return an error And stop $ this-> Halt ("get_undefined: unable to load $ handle."); Return false;} preg_match_all ("// {([^}] ) /} /", $ this-> get_var ($ handle ), $ m); $ m = $ m [1]; // If you can't find the matching text, return an error if (! is_ARRAY ($ m)) Return False;

// If you can find non-empty characters in braces, the value is used as the key value, which makes up an array reset ($ m); while (List ($ k, $ v) = Each ($ m)) {IF ( ! isset ($ this-> varkeys [$ v])) $ Result [$ V] = $ V;} // This array is not empty, otherwise it returns an error if (count ($ result) Return $ Else Return False;

/ / The method of this method is to complete the final processing of $ STR, using the attribute $ unknowns of the class to determine the processing of dynamic parts that cannot be processed in the template ($ Str) {switch ($ this-> unknowns) ) {CASE "Keep": // Keep remains unchanged by Break;

Case "remove": // remove Removes all non-control characters $ str = preg_replace ('/ {[^ / t / r / n}] } /', ",", $ STR); Break;

Case "comment": // Comment Replace the dynamic part of the braces to html Note $ Str = preg_replace ('/ {([^ / T / R / 1]) } /', "

", $ STR); Break;} Return $ Str;}

// This method processes the value of the value in the Varvals array corresponding to the parameter variable to output Function P ($ VARNAME) {Print $ this-> finish ($ this-> get_var ($ varname));}

// This method returns Function Get after the value of the value of the VARVALs array corresponding to the parameter variable ($ VarName) {Return $ this-> finish ($ this-> get_var ($ varname));}

// The method of this method is to check and add a given file name Function FileName ($ filename) {if (Substr ($ filename, 0, 1)! = "/") {// If the file name is not / start, Then it is a relative path, supplement it into a complete absolute path $ filename = $ this-> root. "/". $ Filename;} // If the file does not exist, stop and output an error message if (! File_exists ($ e FileName)) $ this-> Halt ("FileName: File $ FileName Does NOT EXIST."); // Returned value for valid file name returname;}

// This method processes the variable name to turn the sensitive characters in the regular expression into an escape character, and plus a large bracket value ($ VARNAME) {Return PREG_QUOTE ("{". $ Varname on both ends of the variable name. "}");

// This method is based on the $ hand loaded file to the key - value of Function LoadFile ($ hand) {// If there is a file that has already loaded $ hand as a handle, return true IF (isset ($ this-> varkeys [ $ Handle]) And! Empty ($ HANDLE)) Return True; if (! isset ($ this-> file [$ handle])) {// If the handle is not set, return an error and Stop $ this-> Halt ("LoadFile: $ Handle IS Not a Valid Handle."); Return False;} $ filename = $ this-> file [$ handle]; // The handle is valid to remove the corresponding file name /// Connect each row of the file to a string $ str = imporde ("", file ($ STR) {// string is empty instruction file empty or does not exist, return errors and Stop $ this-> Halt ("LoadFile: While Loading $ Handle, $ FileName Does NOT EXIST or IS EMPTY."); Return False;

// If the file is not empty, use $ hand as a handle, $ str for the variable name, add a new button to the key - value pair - value $ this-> set_var ($ Handle, $ Str); return true; }

// This method stops the program runs and gives an error message Function Halt ($ msg) {$ this-> last_error = $ msg;

IF ($ this-> halt_on_error! = "no") $ this-> haldmsg ($ msg); if ($ this-> halt_on_error == "yes") DIE ("halted."); return false;}

// This method gives an error message Function Haltmsg ($ msg) {Printf ("Template Error:% S / R / N", $ MSG);}}?>

转载请注明原文地址:https://www.9cbs.com/read-116692.html

New Post(0)