PHP Classes

File: test.php

Recommend this page to a friend!
  Classes of Gregory   PHTML Template   test.php   Download  
File: test.php
Role: Example script
Content type: text/plain
Description: /test.php
Class: PHTML Template
Perl HTML::Template compatible template compiler
Author: By
Last change: *
Date: 19 years ago
Size: 3,107 bytes
 

Contents

Class file image Download
<?
/*
  PHTML::Template class
  Copiright (C) 2003, 2004, Gregory A. Rozanoff

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.
  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

class tpl {
 var
    
$TEMPLATE,
   
$tplExt = ".tpl";

 
// Front-End functions

 
function tpl($tpl) {
   
$this->templateDir = PATH."/templates/";
   
$this->cacheDir = "d:/localhost/tmp/cache/tpl/"; // Change it!!!
   
$this->start = $this->_getmicrotime();
   
$this->TEMPLATE = $this->templateDir.$tpl.$this->tplExt;
   
$this->CACHE = $this->cacheDir.$tpl.$this->tplExt;
    if (
DEBUG) @unlink($this->CACHE);
 }

 function
done($_root = 'root') {
    global $
$_root;
   
header("Content-Type: text/html; charset=".CHARSET);
   
ob_start(GZIP ? 'ob_gzhandler' : NULL);
   
ob_implicit_flush(FALSE);
    if (!
file_exists($this->CACHE)) $this->_compile();
    foreach ($
$_root as $key => $val) $$key = $val;
    require
$this->CACHE;
   
$uptime = round(($this->_getmicrotime() - $this->start) * 1000000) / 1000;
    echo
"\n\t<!-- ".$uptime." mS -->";
   
ob_end_flush();
    unset(
$this);
 }

 
// Back-End functions
 
 
function _compile() {
   
$tpl = $this->summon($this->TEMPLATE);
    while (
preg_match("/<TMPL_INCLUDE\s+([^\s>]+)\s*>/i", $tpl, $regs))
       
$tpl = str_replace($regs[0], $this->summon($this->templateDir.$regs[1].$this->tplExt), $tpl);
    while (
preg_match("/<TMPL_LOOP\s+(\w+)\s*>/i", $tpl, $r))
       
$tpl = str_replace($r[0], "<?reset(\$".$r[1].");foreach(\$".$r[1]." as \$".$r[1]."_item){if(is_array(\$".$r[1]."_item))foreach(\$".$r[1]."_item as \$key=>\$val) \$\$key=\$val;?>", $tpl);
    while (
preg_match("/<TMPL_(IF|UNLESS)\s+([^\s>]+)\s*>/i", $tpl, $r))
       
$tpl = str_replace($r[0], "<?if(".('UNLESS' == strtoupper($r[1]) ? "!" : "")."\$".$r[2]."){?>", $tpl);
   
$tpl = str_replace("<TMPL_ELSE>", "<?}else{?>", $tpl);
   
$tpl = preg_replace("/<\/TMPL_(IF|UNLESS|LOOP)>/i", "<?}?>", $tpl);
   
$tpl = preg_replace("/<TMPL_VAR\s+([^\s>]+)\s*>/i", "<?=$\\1?>", $tpl);
   
$tpl = ereg_replace("\r", "", $tpl);
    if (
STRICT) {
       
$tpl = preg_replace("/>[\r\t\n\s]+</si", "><", $tpl);
       
$tpl = preg_replace("/<\!--([^>]*?)-->/si", "", $tpl);
    }
   
$fp = fopen($this->CACHE, 'w'); fputs($fp, $tpl); fclose($fp);
 }

 function
summon($fil) {
    if (
$fp = @fopen($fil, "rb")) {
       
$content = fread($fp, 1024*1024);
       
fclose($fp);
        return
$content;
    } return
FALSE;
 }

 function
_getmicrotime() {
    list(
$usec, $sec) = explode(" ",microtime());
    return ((float)
$usec + (float)$sec);
 }

}
?>