一个PHP Template

  1. <?php
  2. class Template {
  3.   var $classname = "Template";
  4.   /* if set, echo assignments */
  5.   var $debug     = false;
  6.   /* $file[handle] = "filename"; */
  7.   var $file  = array();
  8.   /* relative filenames are relative to this pathname */
  9.   var $root   = "";
  10.   /* $varkeys[key] = "key"; $varvals[key] = "value"; */
  11.   var $varkeys = array();
  12.   var $varvals = array();
  13.   /* "remove"  => remove undefined variables
  14.    * "comment" => replace undefined variables with comments
  15.    * "keep"    => keep undefined variables
  16.    */
  17.   var $unknowns = "remove";
  18.   
  19.   /* "yes" => halt, "report" => report error, continue, "no" => ignore error quietly */
  20.   var $halt_on_error  = "yes";
  21.   
  22.   /* last error message is retained here */
  23.   var $last_error     = "";
  24.   /***************************************************************************/
  25.   /* public: Constructor.
  26.    * root:     template directory.
  27.    * unknowns: how to handle unknown variables.
  28.    */
  29.   function Template($root = "."$unknowns = "remove") {
  30.     $this->set_root($root);
  31.     $this->set_unknowns($unknowns);
  32.   }
  33.   /* public: setroot(pathname $root)
  34.    * root:   new template directory.
  35.    */  
  36.   function set_root($root) {
  37.     if (!is_dir($root)) {
  38.       $this->halt("set_root: $root is not a directory.");
  39.       return false;
  40.     }
  41.     
  42.     $this->root = $root;
  43.     return true;
  44.   }
  45.   /* public: set_unknowns(enum $unknowns)
  46.    * unknowns: "remove", "comment", "keep"
  47.    *
  48.    */
  49.   function set_unknowns($unknowns = "keep") {
  50.     $this->unknowns = $unknowns;
  51.   }
  52.   /* public: set_file(array $filelist)
  53.    * filelist: array of handle, filename pairs.
  54.    *
  55.    * public: set_file(string $handle, string $filename)
  56.    * handle: handle for a filename,
  57.    * filename: name of template file
  58.    */
  59.   function set_file($handle$filename = "") {
  60.     if (!is_array($handle)) {
  61.       if ($filename == "") {
  62.         $this->halt("set_file: For handle $handle filename is empty.");
  63.         return false;
  64.       }
  65.       $this->file[$handle] = $this->filename($filename);
  66.     } else {
  67.       reset($handle);
  68.       while(list($h$f) = each($handle)) {
  69.         $this->file[$h] = $this->filename($f);
  70.       }
  71.     }
  72.   }
  73.   /* public: set_block(string $parent, string $handle, string $name = "")
  74.    * extract the template $handle from $parent, 
  75.    * place variable {$name} instead.
  76.    */
  77.   function set_block($parent$handle$name = "") {
  78.     if (!$this->loadfile($parent)) {
  79.       $this->halt("subst: unable to load $parent.");
  80.       return false;
  81.     }
  82.     if ($name == "")
  83.       $name = $handle;
  84.     $str = $this->get_var($parent);
  85.     $reg = "/<!--/s+BEGIN $handle/s+-->(.*)/n/s*<!--/s+END $handle/s+-->/sm";
  86.     preg_match_all($reg$str$m);
  87.     $str = preg_replace($reg"{" . "$name}"$str);
  88.     $this->set_var($handle$m[1][0]);
  89.     $this->set_var($parent$str);
  90.   }
  91.   
  92.   /* public: set_var(array $values)
  93.    * values: array of variable name, value pairs.
  94.    *
  95.    * public: set_var(string $varname, string $value)
  96.    * varname: name of a variable that is to be defined
  97.    * value:   value of that variable
  98.    */
  99.   function set_var($varname$value = "") {
  100.     if (!is_array($varname)) {
  101.       if (!emptyempty($varname))
  102.         if ($this->debug) print "scalar: set *$varname* to *$value*<br>/n";
  103.         $this->varkeys[$varname] = "/".$this->varname($varname)."/";
  104.         $this->varvals[$varname] = $value;
  105.     } else {
  106.       reset($varname);
  107.       while(list($k$v) = each($varname)) {
  108.         if (!emptyempty($k))
  109.           if ($this->debug) print "array: set *$k* to *$v*<br>/n";
  110.           $this->varkeys[$k] = "/".$this->varname($k)."/";
  111.           $this->varvals[$k] = $v;
  112.       }
  113.     }
  114.   }
  115.   /* public: subst(string $handle)
  116.    * handle: handle of template where variables are to be substituted.
  117.    */
  118.   function subst($handle) {
  119.     if (!$this->loadfile($handle)) {
  120.       $this->halt("subst: unable to load $handle.");
  121.       return false;
  122.     }
  123.     $str = $this->get_var($handle);
  124.     $str = @preg_replace($this->varkeys, $this->varvals, $str);
  125.     return $str;
  126.   }
  127.   
  128.   /* public: psubst(string $handle)
  129.    * handle: handle of template where variables are to be substituted.
  130.    */
  131.   function psubst($handle) {
  132.     print $this->subst($handle);
  133.     
  134.     return false;
  135.   }
  136.   /* public: parse(string $target, string $handle, boolean append)
  137.    * public: parse(string $target, array  $handle, boolean append)
  138.    * target: handle of variable to generate
  139.    * handle: handle of template to substitute
  140.    * append: append to target handle
  141.    */
  142.   function parse($target$handle$append = false) {
  143.     if (!is_array($handle)) {
  144.       $str = $this->subst($handle);
  145.       if ($append) {
  146.         $this->set_var($target$this->get_var($target) . $str);
  147.       } else {
  148.         $this->set_var($target$str);
  149.       }
  150.     } else {
  151.       reset($handle);
  152.       while(list($i$h) = each($handle)) {
  153.         $str = $this->subst($h);
  154.         $this->set_var($target$str);
  155.       }
  156.     }
  157.     
  158.     return $str;
  159.   }
  160.   
  161.   function pparse($target$handle$append = false) {
  162.     print $this->parse($target$handle$append);
  163.     return false;
  164.   }
  165.   
  166.   /* public: get_vars()
  167.    */
  168.   function get_vars() {
  169.     reset($this->varkeys);
  170.     while(list($k$v) = each($this->varkeys)) {
  171.       $result[$k] = $this->varvals[$k];
  172.     }
  173.     
  174.     return $result;
  175.   }
  176.   
  177.   /* public: get_var(string varname)
  178.    * varname: name of variable.
  179.    *
  180.    * public: get_var(array varname)
  181.    * varname: array of variable names
  182.    */
  183.   function get_var($varname) {
  184.     if (!is_array($varname)) {
  185.       return $this->varvals[$varname];
  186.     } else {
  187.       reset($varname);
  188.       while(list($k$v) = each($varname)) {
  189.         $result[$k] = $this->varvals[$k];
  190.       }
  191.       
  192.       return $result;
  193.     }
  194.   }
  195.   
  196.   /* public: get_undefined($handle)
  197.    * handle: handle of a template.
  198.    */
  199.   function get_undefined($handle) {
  200.     if (!$this->loadfile($handle)) {
  201.       $this->halt("get_undefined: unable to load $handle.");
  202.       return false;
  203.     }
  204.     
  205.     preg_match_all("//{([^}]+)/}/"$this->get_var($handle), $m);
  206.     $m = $m[1];
  207.     if (!is_array($m))
  208.       return false;
  209.     reset($m);
  210.     while(list($k$v) = each($m)) {
  211.       if (!isset($this->varkeys[$v]))
  212.         $result[$v] = $v;
  213.     }
  214.     
  215.     if (count($result))
  216.       return $result;
  217.     else
  218.       return false;
  219.   }
  220.   /* public: finish(string $str)
  221.    * str: string to finish.
  222.    */
  223.   function finish($str) {
  224.     switch ($this->unknowns) {
  225.       case "keep":
  226.       break;
  227.       
  228.       case "remove":
  229.         $str = preg_replace('/{[^ /t/r/n}]+}/'""$str);
  230.       break;
  231.       case "comment":
  232.         $str = preg_replace('/{([^ /t/r/n}]+)}/'"<!-- Template $handle: Variable //1 undefined -->"$str);
  233.       break;
  234.     }
  235.     
  236.     return $str;
  237.   }
  238.   /* public: p(string $varname)
  239.    * varname: name of variable to print.
  240.    */
  241.   function p($varname) {
  242.     print $this->finish($this->get_var($varname));
  243.   }
  244.   function get($varname) {
  245.     return $this->finish($this->get_var($varname));
  246.   }
  247.     
  248.   /***************************************************************************/
  249.   /* private: filename($filename)
  250.    * filename: name to be completed.
  251.    */
  252.   function filename($filename) {
  253.     if (substr($filename, 0, 1) != "/") {
  254.       $filename = $this->root."/".$filename;
  255.     }
  256.     
  257.     if (!file_exists($filename))
  258.       $this->halt("filename: file $filename does not exist.");
  259.     return $filename;
  260.   }
  261.   
  262.   /* private: varname($varname)
  263.    * varname: name of a replacement variable to be protected.
  264.    */
  265.   function varname($varname) {
  266.     return preg_quote("{".$varname."}");
  267.   }
  268.   /* private: loadfile(string $handle)
  269.    * handle:  load file defined by handle, if it is not loaded yet.
  270.    */
  271.   function loadfile($handle) {
  272.     if (isset($this->varkeys[$handle]) and !emptyempty($this->varvals[$handle]))
  273.       return true;
  274.     if (!isset($this->file[$handle])) {
  275.       $this->halt("loadfile: $handle is not a valid handle.");
  276.       return false;
  277.     }
  278.     $filename = $this->file[$handle];
  279.     $str = implode("", @file($filename));
  280.     if (emptyempty($str)) {
  281.       $this->halt("loadfile: While loading $handle, $filename does not exist or is empty.");
  282.       return false;
  283.     }
  284.     $this->set_var($handle$str);
  285.     
  286.     return true;
  287.   }
  288.   /***************************************************************************/
  289.   /* public: halt(string $msg)
  290.    * msg:    error message to show.
  291.    */
  292.   function halt($msg) {
  293.     $this->last_error = $msg;
  294.     
  295.     if ($this->halt_on_error != "no")
  296.       $this->haltmsg($msg);
  297.     
  298.     if ($this->halt_on_error == "yes")
  299.       die("<b>Halted.</b>");
  300.     
  301.     return false;
  302.   }
  303.   
  304.   /* public, override: haltmsg($msg)
  305.    * msg: error message to show.
  306.    */
  307.   function haltmsg($msg) {
  308.     printf("<b>Template Error:</b> %s<br>/n"$msg);
  309.   }
  310. }
  311.   function savetofile ($dir,$varname){
  312.    $data=$this->finish($this->get_var($varname));
  313.    $fp=fopen($dir,"w+");
  314.    fwrite($fp,$data);
  315.   }
  316.    function renew(){
  317.     $this->varkeys=array();
  318.     $this->varvals=array();
  319.     $this->file=array();
  320.     }
  321. ?>

 测试页面:

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  5. <title>无标题文档</title>
  6. </head>
  7. <body>
  8. <?php
  9.  require("template.inc");
  10.  faction staticinfo ($id){
  11.   mysql_connection(localhost, root,);
  12.   mysql_select_db (dbname);
  13.   global $table,$template,$itpl
  14.   $rs=mysql_query("select id from $table where id=$id");
  15.   $array=mysql_fetch_query("$rs");
  16.   $target=$array[“target”];
  17.   $title=$array[“title”];
  18.   $itpl=new template;
  19.   
  20.   //分析模板
  21.   $tpl->set_file(“main”,$template);
  22.   //把模板中的{title}变量换成$title
  23.   $itpl->set_var(“title”,$title”);
  24.   //分析整个模板
  25.   $itpl->set_var(“mains”,”main”);
  26.   //把mains写入文件
  27.   $tpl->savetofile($target,"mains");
  28.   //置空
  29.   $tpl->renew();
  30. ?>
  31. </body>
  32. </html>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值