phplibテンプレートクラス(template)

30188 ワード

phplibテンプレートクラス templateはSmartyより 古いphpテンプレートも
基本的に試用は廃棄されています(ただし、会社設立が早いので、これは存在します...)
**********************************************************************************************************************
使用方法:https://blog.csdn.net/gaokcl/article/details/84063918
0,重要:smartyと phplibテンプレートクラス(template) ,新旧テンプレート結合
$head_meta = $tplEngine->fetch("common/head_meta.html");
$header = $tplEngine->fetch("common/header.html");
$foot = $tplEngine->fetch("common/foot.html");
$foot_script = $tplEngine->fetch("common/foot_script.html");

$temp -> set_var("head_meta", $head_meta);
$temp -> set_var("header", $header);
$temp -> set_var("foot", $foot);
$temp -> set_var("foot_script", $foot_script);

***********************************************************************************************************************
template.class.php:  (英語版【短い、もっと古い】と中国語解釈版【長い、解釈はネットなので、見ましょう】)
一、英語版:
 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 = "";


    /***************************************************************************/
    /* public: Constructor.
     * root:     template directory.
     * unknowns: how to handle unknown variables.
     */
    function Template($root = ".", $unknowns = "remove")
    {
        $this->set_root($root);
        $this->set_unknowns($unknowns);
    }

    /* public: setroot(pathname $root)
     * root:   new template 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;
    }

    /* public: set_unknowns(enum $unknowns)
     * unknowns: "remove", "comment", "keep"
     *
     */
    function set_unknowns($unknowns = "keep")
    {
        $this->unknowns = $unknowns;
    }

    /* public: set_file(array $filelist)
     * filelist: array of handle, filename pairs.
     *
     * public: set_file(string $handle, string $filename)
     * handle: handle for a filename,
     * filename: name of template file
     */
    function set_file($handle, $filename = "")
    {
        if (!is_array($handle)) {
            if ($filename == "") {
                $this->halt("set_file: For handle $handle filename is empty.");
                return false;
            }
            $this->file[$handle] = $this->filename($filename);
        } else {
            reset($handle);
            while (list($h, $f) = each($handle)) {
                $this->file[$h] = $this->filename($f);
            }
        }
    }

    /* public: set_block(string $parent, string $handle, string $name = "")
     * extract the template $handle from $parent,
     * place variable {$name} instead.
     */
    function set_block($parent, $handle, $name = "")
    {
        if (!$this->loadfile($parent)) {
            $this->halt("subst: unable to load $parent.");
            return false;
        }
        if ($name == "")
            $name = $handle;

        $str = $this->get_var($parent);
        $reg = "/(.*)
\s*/sm"; preg_match_all($reg, $str, $m); $str = preg_replace($reg, "{" . "$name}", $str); $this->set_var($handle, $m[1][0]); $this->set_var($parent, $str); } /* public: set_var(array $values) * values: array of variable name, value pairs. * * public: set_var(string $varname, string $value) * varname: name of a variable that is to be defined * value: value of that variable */ function set_var($varname, $value = "") { if (!is_array($varname)) { if (!empty($varname)) if ($this->debug) print "scalar: set *$varname* to *$value*

"; $this->varkeys[$varname] = "/" . $this->varname($varname) . "/"; $this->varvals[$varname] = $value; } else { reset($varname); while (list($k, $v) = each($varname)) { if (!empty($k)) if ($this->debug) print "array: set *$k* to *$v*

"; $this->varkeys[$k] = "/" . $this->varname($k) . "/"; $this->varvals[$k] = $v; } } } /* public: subst(string $handle) * handle: handle of template where variables are to be substituted. */ function subst($handle) { if (!$this->loadfile($handle)) { $this->halt("subst: unable to load $handle."); return false; } $str = $this->get_var($handle); $str = @preg_replace($this->varkeys, $this->varvals, $str); 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($handle); if ($append) { $this->set_var($target, $this->get_var($target) . $str); } else { $this->set_var($target, $str); } } else { reset($handle); while (list($i, $h) = each($handle)) { $str = $this->subst($h); $this->set_var($target, $str); } } return $str; } function pparse($target, $handle, $append = false) { print $this->parse($target, $handle, $append); return false; } /* public: get_vars() */ function get_vars() { reset($this->varkeys); while (list($k, $v) = each($this->varkeys)) { $result[$k] = $this->varvals[$k]; } return $result; } /* public: get_var(string varname) * varname: name of variable. * * public: get_var(array varname) * varname: array of variable names */ function get_var($varname) { if (!is_array($varname)) { if (isset($this->varvals[$varname])) { return $this->varvals[$varname]; } } else { reset($varname); while (list($k, $v) = each($varname)) { $result[$k] = $this->varvals[$k]; } return $result; } } /* public: get_undefined($handle) * handle: handle of a template. */ function get_undefined($handle) { if (!$this->loadfile($handle)) { $this->halt("get_undefined: unable to load $handle."); return false; } preg_match_all("/\{([^}]+)\}/", $this->get_var($handle), $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; } if (count($result)) return $result; else return false; } /* public: finish(string $str) * str: string to finish. */ function finish($str) { switch ($this->unknowns) { case "keep": break; case "remove": $str = preg_replace('/{[^ \t\r
}]+}/', "", $str); break; case "comment": $str = preg_replace('/{([^ \t\r
}]+)}/', "", $str); break; } return $str; } /* public: p(string $varname) * varname: name of variable to print. */ function p($varname) { print $this->finish($this->get_var($varname)); } function get($varname) { return $this->finish($this->get_var($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($varname) * varname: name of a replacement variable to be protected. */ function varname($varname) { return preg_quote("{" . $varname . "}"); } /* private: loadfile(string $handle) * handle: load file defined by handle, if it is not loaded yet. */ function loadfile($handle) { if (isset($this->varkeys[$handle]) and !empty($this->varvals[$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($filename)); if (empty($str)) { $this->halt("loadfile: While loading $handle, $filename does not exist or is empty."); return false; } $this->set_var($handle, $str); return true; } /***************************************************************************/ /* public: halt(string $msg) * msg: error message to show. */ function halt($msg) { $this->last_error = $msg; if ($this->halt_on_error != "no") $this->haltmsg($msg); if ($this->halt_on_error == "yes") die("Halted."); return false; } /* public, override: haltmsg($msg) * msg: error message to show. */ function haltmsg($msg) { printf("Template Error: %s

", $msg); } }  
****************************************************************************************************************
  , :
          || "comment" =>             || "keep" =>         
    var $halt_on_error = "yes"; // "yes" =>    || "report" =>     ,     || "no" =>     
    var $last_error = "";

    /***************************************************************************************/

    /**
     * @       Template
     *
     * Template constructor.
     * @param string $root
     * @param string $unknowns
     */
    function Template($root = ".", $unknowns = "remove")
    {
        if ($this->debug & 4) {
            echo "

: root = $root, unknowns = $unknowns

/n"; } $this->set_root($root); // $this->set_unknowns($unknowns); // unknowns "remove" } /** * set_root * * @param $root * @return bool */ function set_root($root) { if ($this->debug & 4) { echo "

: root = $root

/n"; } if (!is_dir($root)) { $this->halt(" : $root ."); return false; } $this->root = $root; return true; } /** * set_unknowns, * * @param string $unknowns */ function set_unknowns($unknowns = "remove") { if ($this->debug & 4) { echo "

: = $unknowns

/n"; } $this->unknowns = $unknowns; } /** * file $varname * * @param $varname * @param string $filename * @return bool */ function set_file($varname, $filename = "") { // varname if (!is_array($varname)) { if ($this->debug & 4) { echo "

: (with scalar) varname = $varname, filename = $filename

/n"; } // , if ($filename == "") { // $this->halt("set_file: For handle $handle filename is empty."); $this->halt(" : $varname ."); return false; } $this->file[$varname] = $this->filename($filename); } else { // varname file reset($varname); // file while (list($v, $f) = each($varname)) { if ($this->debug & 4) { echo "

set_file: (with array) varname = $v, filename = $f

/n"; } if ($f == "") { $this->halt("set_file: For varname $v filename is empty."); return false; } $this->file[$v] = $this->filename($f); } } return true; } /** * public: set_block(string $parent, string $handle, string $name = "") * , {$$name}。 * * @param $parent * @param $varname * @param string $name * @return bool */ function set_block($parent, $varname, $name = "") { if ($this->debug & 4) { echo "

set_block: parent = $parent, varname = $varname, name = $name

/n"; } if (!$this->loadfile($parent)) { $this->halt("set_block: unable to load $parent."); return false; } if ($name == "") { $name = $varname;// , } $str = $this->get_var($parent); $reg = "/[ /t]*/s*?/n?(/s*.*?/n?)/s*/s*?/n?/sm"; preg_match_all($reg, $str, $m); $str = preg_replace($reg, "{ " . "$name }", $str); $this->set_var($varname, $m[1][0]); $this->set_var($parent, $str); return true; } /** * Varname varkeys * * public: set_var(array $values) * values: ( , ) * public: set_var(string $varname, string $value) * varname: * value: */ function set_var($varname, $value = "", $append = false) { // if (!is_array($varname)) { // if (!empty($varname)) { if ($this->debug & 1) { printf("set_var: (with scalar) %s = '%s'
/n", $varname, htmlentities($value)); } $this->varkeys[$varname] = "/" . $this->varname($varname) . "/"; if ($append && isset($this->varvals[$varname])) { $this->varvals[$varname] .= $value; } else { $this->varvals[$varname] = $value; } } /****************/ } else { reset($varname); while (list($k, $v) = each($varname)) { if (!empty($k)) { if ($this->debug & 1) { printf("set_var: (with array) %s = '%s'
/n", $k, htmlentities($v)); } $this->varkeys[$k] = "/" . $this->varname($k) . "/"; if ($append && isset($this->varvals[$k])) { $this->varvals[$k] .= $v; } else { $this->varvals[$k] = $v; } } } } } /** * clear_var * * @param $varname */ function clear_var($varname) { // varname if (!is_array($varname)) { if (!empty($varname)) { if ($this->debug & 1) { printf("clear_var: (with scalar) %s
/n", $varname); } $this->set_var($varname, ""); } } else { reset($varname); while (list($k, $v) = each($varname)) { if (!empty($v)) { if ($this->debug & 1) { printf("clear_var: (with array) %s
/n", $v); } $this->set_var($v, ""); } } } } /** * unset_var, * * @param $varname */ function unset_var($varname) { if (!is_array($varname)) { if (!empty($varname)) { if ($this->debug & 1) { printf("unset_var: (with scalar) %s
/n", $varname); } unset($this->varkeys[$varname]); unset($this->varvals[$varname]); } } else { reset($varname); while (list($k, $v) = each($varname)) { if (!empty($v)) { if ($this->debug & 1) { printf("unset_var: (with array) %s
/n", $v); } unset($this->varkeys[$v]); unset($this->varvals[$v]); } } } } /** * , * * @param $varname * @return bool|mixed|null|string|string[] */ function subst($varname) { $varvals_quoted = array(); if ($this->debug & 4) { echo "

subst: varname = $varname

/n"; } // , if (!$this->loadfile($varname)) { $this->halt("subst: unable to load $varname."); return false; } reset($this->varvals); while (list($k, $v) = each($this->varvals)) { $varvals_quoted[$k] = preg_replace(array('//', '//$/'), array('//', '$/'), $v); } // $str = $this->get_var($varname); $str = preg_replace($this->varkeys, $varvals_quoted, $str); return $str; } /** * subst, * * @param $varname * @return bool */ function psubst($varname) { if ($this->debug & 4) { echo "

psubst: varname = $varname

/n"; } print $this->subst($varname); return false; } /** * varname * target varvals * sub * * @param $target * @param $varname * @param bool $append * @return bool|mixed|null|string|string[] */ function parse($target, $varname, $append = false) { if (!is_array($varname)) { if ($this->debug & 4) { echo "

parse: (with scalar) target = $target, varname = $varname, append = $append

/n"; } $str = $this->subst($varname); if ($append) { $this->set_var($target, $this->get_var($target) . $str); } else { $this->set_var($target, $str); } } else { reset($varname); while (list($i, $v) = each($varname)) { if ($this->debug & 4) { echo "

parse: (with array) target = $target, i = $i, varname = $v, append = $append

/n"; } $str = $this->subst($v); if ($append) { $this->set_var($target, $this->get_var($target) . $str); } else { $this->set_var($target, $str); } } } if ($this->debug & 4) { echo "

parse: completed

/n"; } return $str; } /** * parse , * * @param $target * @param $varname * @param bool $append * @return bool */ function pparse($target, $varname, $append = false) { if ($this->debug & 4) { echo "

pparse: passing parameters to parse...

/n"; } print $this->finish($this->parse($target, $varname, $append)); return false; } /** * , * * @return mixed */ function get_vars() { if ($this->debug & 4) { echo "

get_vars: constructing array of vars...

/n"; } reset($this->varkeys); while (list($k, $v) = each($this->varkeys)) { $result[$k] = $this->get_var($k); } return $result; } /** * , * * @param $varname * @return mixed|string */ function get_var($varname) { // if (!is_array($varname)) { // if (isset($this->varvals[$varname])) { $str = $this->varvals[$varname]; } else { $str = ""; } if ($this->debug & 2) { printf("get_var (with scalar) %s = '%s'
/n", $varname, htmlentities($str)); } return $str; } else { reset($varname); while (list($k, $v) = each($varname)) { if (isset($this->varvals[$v])) { $str = $this->varvals[$v]; } else { $str = ""; } if ($this->debug & 2) { printf("get_var: (with array) %s = '%s'
/n", $v, htmlentities($str)); } $result[$v] = $str; } return $result; } } /** * , * * @param $varname * @return bool */ function get_undefined($varname) { if ($this->debug & 4) { echo "

get_undefined: varname = $varname

/n"; } if (!$this->loadfile($varname)) { $this->halt("get_undefined: unable to load $varname."); return false; } preg_match_all("/{ ([^ /t/r/n }]+) }/", $this->get_var($varname), $m); $m = $m[1]; // , if (!is_array($m)) { return false; } // , , reset($m); while (list($k, $v) = each($m)) { if (!isset($this->varkeys[$v])) { if ($this->debug & 4) { echo "

get_undefined: undefined: $v

/n"; } $result[$v] = $v; } } // , if (count($result)) { return $result; } else { return false; } } /** * str , unknowns * * @param $str * @return null|string|string[] */ function finish($str) { switch ($this->unknowns) { case "keep": // break; case "remove": // $str = preg_replace('/{ [^ /t/r/n }]+ }/', "", $str); break; case "comment":// HTML $str = preg_replace('/{ ([^ /t/r/n }]+) }/', "", $str); break; } return $str; } /** * * * @param $varname */ function p($varname) { print $this->finish($this->get_var($varname)); } /** * * * @param $varname * @return null|string|string[] */ function get($varname) { return $this->finish($this->get_var($varname)); } /** * * * @param $filename * @return string */ function filename($filename) { if ($this->debug & 4) { echo "

filename: filename = $filename

/n"; } // , , if (substr($filename, 0, 1) != "/") { $filename = $this->root . "/" . $filename; } // if (!file_exists($filename)) { $this->halt("filename: file $filename does not exist."); } return $filename;// } /** * , , * * @param $varname * @return string */ function varname($varname) { // preg_quote, str return preg_quote("{ " . $varname . " }"); } /** * varname * * @param $varname * @return bool */ function loadfile($varname) { if ($this->debug & 4) { echo "

loadfile: varname = $varname

/n"; } // if (!isset($this->file[$varname])) { // $varname does not reference a file so return if ($this->debug & 4) { echo "

loadfile: varname $varname does not reference a file

/n"; } return true; } // varname , if (isset($this->varvals[$varname])) { if ($this->debug & 4) { echo "

loadfile: varname $varname is already loaded

/n"; } return true; } // $filename = $this->file[$varname]; // $str = implode("", @file($filename)); // , if (empty($str)) { $this->halt("loadfile: While loading $varname, $filename does not exist or is empty."); return false; } if ($this->debug & 4) { printf("loadfile: loaded $filename into $varname
/n"); } // , $varname ,str // $this->set_var($varname, $str); return true; } /** * * * @param $dir * @param $varname */ function savetofile($dir, $varname) { $data = $this->finish($this->get_var($varname)); $fp = fopen($dir, "w+"); fwrite($fp, $data); } /** * */ function renew() { $this->varkeys = array(); $this->varvals = array(); $this->file = array(); } /** * * * @param $msg * @return bool */ function halt($msg) { $this->last_error = $msg; if ($this->halt_on_error != "no") { $this->haltmsg($msg); } if ($this->halt_on_error == "yes") { die(" ."); } return false; } /** * * * @param $msg */ function haltmsg($msg) { printf(" : %s
/n", $msg); } }