Add two functions, usage, and the original, specifically see the code. Just in the statement, you want to specify the path to the image, the default is the current directory, that is, the directory where the PHP file is located. Note that the picture directory is in the same class directory relative to the template file directory. Source: http://blog.9cbs.net/kingerq Php / * * session management for php3 * (c) Copyright 1999-2000 Netuse GmbH * Kristian KoehNTopp * * $ ID: Template.inc, V 1.5 2000 / 07/12 18:22:35 KK EXP $ * * /
Class Template {var $ classname = "template";
/ * if set, echo assignments * / var $ debug = false;
/ * $ file [hand] = "filename"; * / var $ file = array ();
/ * Relative Filenames Are Relative to this Pathname * / var $ root = ""
/ * $ VARKEYS [key] = "key"; $ VARVALS [key] = "value"; * / var $ varK = array (); var $ varvals = array ();
/ * "remove" => remove undefined variables * "comment" => replace undefined variables with comments * "keep" => keep undefined variables * / var $ unknowns = "remove"; / * "yes" => halt, " Report "=> Report error, continue," no "=> ignore error quietly * / var $ halt_on_error =" yes "; / * last error message is retained here * / var $ last_error =" "
VAR $ PATH = "." / ************************************************** ************************************************************** / / * PUBLIC: Constructor. * Root: Template Directory. * UNKNOWNS: How to handle unknown variables. * / Function Template ($ root = ", $ path =". "$ unknowns =" remove ") {$ this-> setRoot ($ root); $ this-> setpath ($ PATH); $ this-> setunknowns ($ unknowns);} / * Set image path * / function setPath ($ PATH) {if ($ PATH == ") Return True; if (! is_dir ($ this-> Root. "/" $ PATH) {$ this-> Halt ("setPath: $ path is not a directory."); Return False;} $ this-> Path = $ PATH; RETURN TRUE;} / * public : setname $ root * root: New template Directory. * / Function set ($ root) {if (! is_dir ($ root) {$ this-> halt ("Stroot: $ root is not a directory." ); $ This-> root = $ root; return true;} / * public: setunknowns (enum $ unknowns) * Unknowns: "remove", "comment", "keep" * * / function setunknowns ($ unknowns = "Keep ") {$ This-> unknowns = $ unknowns;}
/ * Public: setfile (array $ filelist) * filelist: array of handle, filename pairs * * public:. Setfile (string $ handle, string $ filename) * handle: handle for a filename, * filename: name of template file * / Function setFile ($ Handle, $ filename = ") {if (! Is_ARRAY) {IF ($ filename ==") {$ this-> halt ("setfile: for handle $ handle filename is Empty Return false;} $ this-> file [$ hand] = $ this-> filename ($ filename);} else {reset ($ hand); while ($ H, $ f) = Each $ Handle)) {$ this-> file [$ h] = $ this-> filename ($ f);}}} / * public: setblock (String $ Parent, String $ Handle, String $ Name = ") * Extract The Template $ Handle from $ Parent, * Place Variable {$ Name} instead. * / Function SetBlock ($ PARENT, $ Handle, $ Name = ") {if (! $ this-> loadingfile ($ pent) { $ this-> Halt ("Subst: Unable to Load $ Parent."); Return False;} IF ($ Name == ") $ name = $ handle;
$ Str = $ this-> getvar ($ pent); $ reg = "/ (. *) / n / s * / SM; preg_match_all ($ R, $ Str, $ M); $ Str = preg_replace ($ reg, "{". "$ name}", $ STR); $ this-> setvar ($ HANDLE, $ M [1] [0]); $ this-> setvar ($ PARENT, $ STR); $ this-> setvar ($ name);} / * public: forblock (String $ Parent, string $ Handle, Array $ VALUES * Extract The Template $ Handle from $ PARENT, * VALUES: Array of Variable Name, Value Pairs. * / Function Forblock ($ PARENT, $ HANDLE, $ VALUES, $ Step = 1) {IF (! $ step = 1; $ postfix = "_postfix"; if (is_ARRAY ($ Handle) {for ($ I = 0; $ i
}} / * Public: setvar (array $ values) * VALUES: Array Of Variable Name, Value Pairs. * * Public: setvar (String $ VarName, String $ Value) * VarName: Name of a variable what to be defined * Value: value of what variable * / function setvar ($ VARNAME, $ VALUE = ") {if (! is_ARRAY ($ VARNAME)) {if (! Empty ($ VarName)) IF ($ this-> debug) Print Scarar: set * $ Varname * to * $ value *
/ n "; $ this-> varkeys [$ varname] =" / "$ @ >varname ($ this" > VARVALS [$ VARNAME] = $ value;} else {reset ($ VARNAME); While ($ K, $ V) = Each ($ VARNAME)) {if (! Empty ($ K)) IF ($ THIS -> Debug) Print "Array: set * $ k * to * $ v *
/ n"; $ this-> varkeys [$ k] = "/". $ t ($)." / "; $ K] = $ v;}}} / * public: subst (string $ hand) * handle: handle of time) * Handle: Handle of Template WHERE VARIABLES ARE To Be Substuted. * / Function Subst ($ Handle) {IF ($ this-> loadfile) {$ this-> halt ("Subst: Unable To load $ handle. "); returnaf
$ Str = $ this-> getvar ($ Handle); $ str = @preg_replace ($ this-> var); Return $ Str;} / * public: psubst (string $ handle) * Handle: Handle of Template WHERE VARIABLES Are to Be Substituted. * / Function Psubst ($ Handle) {Print $ this-> Subst ($ Handle); Return False;
/ * Public: parse (string $ target, string $ handle, boolean append) * public: parse (string $ target, array $ handle, boolean append) * target: handle of variable to generate * handle: handle of template to substitute * Append: append to target handle * / function parse ($ TARGET, $ HANDLE, $ append = false) {if (! is_ARRAY ($ handle)) {$ str = $ this-> subst ($ hand); if ($ append) ) {$ THIS-> Setvar ($ TARGET, $ THIS-> Getvar ($ target). $ Str);} else {$ this-> setvar ($ TARGET, $ STR);}} else {reset ($ handle) While ($ I, $ H) = Each ($ handle)) {$ str = $ this-> Subst ($ H); $ this-> setvar ($ TARGET, $ STR);}} RETURN $ STR Function PPARSE ($ TARGET, $ HANDLE, $ append = false) {Print $ this-> Parse ($ TARGET, $ Handle, $ append); Return False;} / * public: getvars () * / Function GetVars ) {RESET ($ this-> varkeys); while ($ k, $ v) = Each ($ this-> varkeys)) {$ Result [$ k] = $ this-> varvals [$ k]; r . Eturn $ result;} / * public: getvar (string varname) * varname: name of variable * * public: getvar (array varname) * varname: array of variable names * / function getvar ($ varname) {if (is_array! ($ VARNAME)) {Return $ this-> VARVALS [$ VarName];} else {reset ($ VarName); While ($ K, $ V) = Each ($ VARNAME)) {$ Result [$ k] = $ this-> varvals [$ k];} return $ results;}} / * public: getundefined ($ hand) * handle: handle of a template. * / function getundefined ($ hand) {ix (! $ this- > LoadFile ($ Handle)) {$ this-> Halt ("getundefined: unable to load $ handle."); Return False;
} PREG_MATCH_ALL ("// ^}] ) /} /", $ this-> getvar ($ hand), $ m); $ m = $ m [1]; if (! Is_ARRAY ($ M) ) Return False; Reset ($ M); while (List ($ k, $ v) = Each ($ m)) {if (! Isset ($ this-> varkeys [$ v]) $ Result [$ V] = $ V;} f (count) Return $ Result; Else Return false;}
/ * public: finish (string $ str) * Str: string to finish. * / function finish ($ str) {switch ($ this-> unknowns) {copy "keep": break; case "remove": $ str = PREG_REPLACE ('/ {[^ / T / R / N}] } /', "", $ STR); Break;
Case "Comment": $ str = preg_replace ('/ {([^ / T / R / N}] )} /', " ", $ Str); Break;} Return $ Str;}
/ * public: p (string $ varname) * Varname: name of variable to print. * / function p ($ VarName) {Print $ this-> finish ($ this-> getvar ($ varname));}
Function GET ($ VARNAME) {Return $ this-> finish ($ this-> getvar ($ varname));} / ********************** *********************************************************** ** / / * private: filename ($ filename) * filename: name to be completed. * / Function filename ($ filename) {if (Substr ($ filename, 0, 1)! = "/") {$ Filename = $ this-> root. "/". $ filename;} if (! File_exists ($ filename)) $ this-> halt ("FileName: File $ FileName Does NOT EXIST.");
Return $ filename;} / * private: VarName: Name of a replacement variable to be protected. * / function varname ($ VARNAME) {Return preg_quote ("{". $ varname. "{"); }
/ * private: loadfile (string $ hand) * handle: loading file defined by handle, if it is not loaded yet. * / function loadfile ($ hand) {ix (isset ($ this-> varkeys [$ handle]) and Empty ($ HANDLE)) RETURN TRUE; if (! isset ($ this-> file [$ Handle])) {$ this-> Halt ("LoadFile: $ Handle Is Not a Valid Handle Return False;} $ filename = $ this-> file [$ handle];
$ Str = Implode ("", @file ($ str)) {$ this-> Halt ("LoadFile: While Loading $ Handle, $ FileName Does Not Exist or Is EMPTY.") Return False;} $ str = $ this-> parsepath ($ str); // Replace the image path
$ this-> setvar ($ Handle, $ Str); Return true;} / * Re-analyzes the specified path in the replacement template (generally picture file path) * / function parsepath ($ str = ") {if (" / "! = Substr ($ this-> path, -1)) {$ this-> path. =" / ";} if (" ./ "== $ this-> path) Return $ Str; Str = preg_replace ("| (/././) *".-> Path." | ", $ this-> root." / ". $ this-> Path, $ STR; RETURN $ STR;}
/ ************************************************** *********************************** / / * PUBLIC: HALT (String $ msg) * msg: error message to show. * / Function halt $ msg) {$ this-> last_error = $ msg; if ($ this-> halt_on_error! = "no") $ this-> haldmsg ($ msg); if ($ this-> halt_on_error == "yes") DIE (" halted. b>"); Return False;} / * public, Override: Haltmsg ($ msg) * msg: error message to show. * / function haltmsg ($ msg) {printf ("< B> Template Error: b>% s
/ n ", $ msg);}}?>