<?php
class Template {
var $strTemplateDir = './templates/2';
var $strCacheDir = './templates/_cached';
var $strBeginTag = '{';
var $strEndTag = '}';
var $arrVars = array();
var $arrValues = array();
var $boolCache = true;
var $strBuffer = null;
function Template( ) {} // pusty konstruktor ;p
function assign( $strVar, $strValue ) {
$this->arrVars[] = $this->strBeginTag . '$' . $strVar . $this->strEndTag;
$this->arrValues[] = $strValue;
}
function clear( ) {
unset( $this->arrVars, $this->arrValues );
}
function display( $strFile ) {
if( $this->boolCache ) {
if( file_exists( $this->strCacheDir . '/' . md5( $strFile ) . '.tpl' ) && filemtime( $this->strCacheDir . '/' . md5( $strFile ) . '.tpl' ) >= time() - 300 ) {
$resFile = fopen( $this->strCacheDir . '/' . md5( $strFile ) . '.tpl', 'r' );
$this->strBuff = fread( $resFile, filesize( $this->strCacheDir . '/' . md5( $strFile ) . '.tpl' ) );
echo $this->strBuff;
fclose( $resFile );
}
else {
$resFile = fopen( $this->strTemplateDir . '/' . $strFile, 'r' );
$strBuff = fread( $resFile, filesize( $this->strTemplateDir . '/' . $strFile ) );
$this->strBuff = str_replace( $this->arrVars, $this->arrValues, $strBuff );
fclose( $resFile );
echo $this->strBuff;
$resFileCache = fopen( $this->strCacheDir . '/' . md5( $strFile ) . '.tpl', 'w' );
fwrite( $resFileCache, $this->strBuff );
}
}
else {
$resFile = fopen( $this->strTemplateDir . '/' . $strFile, 'r' );
$strBuff = fread( $resFile, filesize( $this->strTemplateDir . '/' . $strFile ) );
$this->strBuff = str_replace( $this->arrVars, $this->arrValues, $strBuff );
fclose( $resFile );
echo $this->strBuff;
}
}
}
?>