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 = "/(.*)\n\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*
\n";
$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*
\n";
$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\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->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
\n", $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);
}
}