here is the tplparser.php code the code above is the conn.php that also connects to the database
<?php
/*******************************************************************************
/////////////// Copyright ©2006 DL PAYCART - www.dinkumsoft.com ////////////////
*******************************************************************************/
/**
* // Load template file //
*
* $tpl = new tplparser;
* $tpl->setDir( 'tpl/' );
* $tpl->dHtml( 'file_name.tpl' );
* $content = $tpl->tHtml( 'file_name.tpl' );
*
* * // Load template block //
* $tpl->dbHtml( 'file_name.tpl', 'HEAD' );
* $content = $tpl->tbHtml( 'file_name.tpl', 'HEAD' );
* <!-- BEGIN HEAD -->
* <!-- END HEAD -->
*
*/
class TplParser
{
var $content;
var $TplFile;
var $block;
var $startBlock;
var $endBlock;
var $endBlockLine;
var $fileContent;
var $blockContent;
var $cache;
var $iso;
var $directory;
function TplParser( ){
$this->content = '';
$this->TplFile = '';
$this->block = '';
$this->startBlock = '<!-- BEGIN_';
$this->endBlock = '<!-- END_';
$this->endBlockLine = ' -->';
$this->cache = true;
$this->iso = true;
} // end function TplParser
function dHtml( $sFile, $bCache = true, $bIso = true ){
$this->setFile( $this->directory.$sFile );
$this->cache = $bCache;
$this->iso = $bIso;
$this->display( );
echo $this->content;
unset( $this->content );
flush( );
} // end function dHtml
function tHtml( $sFile, $bCache = true, $bIso = true ){
$this->setFile( $this->directory.$sFile );
$this->cache = $bCache;
$this->iso = $bIso;
$this->display( );
return $this->content;
} // end function tHtml
function dbHtml( $sFile, $sBlock, $bCache = true, $bIso = true ){
$this->setFile( $this->directory.$sFile );
$this->setBlock( $sBlock );
$this->cache = $bCache;
$this->iso = $bIso;
$this->display( true );
echo $this->content;
unset( $this->content );
flush( );
} // end function dbHtml
function tbHtml( $sFile, $sBlock, $bCache = true, $bIso = true ){
$this->setFile( $this->directory.$sFile );
$this->setBlock( $sBlock );
$this->cache = $bCache;
$this->iso = $bIso;
$this->display( true );
return $this->content;
} // end function tbHtml
function display( $bBlock = null ){
if( $this->checkFile( ) ){
if( isset( $bBlock ) )
$this->blockParse( );
else
$this->allParse( );
}
} // end function display
function checkFile( ){
if( is_file( $this->TplFile ) ){
return true;
}
else {
$this->content = null;
//echo 'No template file: <i>'.$this->TplFile.'</i><br />';
return false;
}
} // end function checkFile
function parse( ){
if( !isset( $poz ) )
$poz[1][1] = 0;
while( strpos( $this->content, '$', $poz[1][1] ) ) {
$poz[1][1] = strpos( $this->content, '$', $poz[1][1] ) + 1;
preg_match( '/\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\[\]x7f-\xff]*/', $this->content, $wyniki );
if( ereg( '\]', $wyniki[0] ) ){
if( ereg( '\[', $wyniki[0] ) ){
$poz[1][2] = $poz[1][1] + strpos( $wyniki[0], ']' );
$bArray = true;
}
else{
$poz[1][2] = $poz[1][1] + strpos( $wyniki[0], ']' ) - 1;
$bArray = null;
}
}
else{
$poz[1][2] = $poz[1][1] + strlen( $wyniki[0] ) - 1;
$bArray = null;
}
$TplVar = substr( $this->content, $poz[1][1], $poz[1][2] - $poz[1][1] );
if( isset( $bArray ) ){
list($var,) = explode( '[', $TplVar );
if( isset( $GLOBALS[$var] ) ){
global $$var;
$poz[2][1]= strpos( $TplVar, '[' )+1;
$poz[2][2]= strpos( $TplVar, ']' );
$key = substr( $TplVar, $poz[2][1], $poz[2][2]-$poz[2][1] );
if( isset( ${$var}[$key] ) )
$tekst = ${$var}[$key];
else
$tekst = null;
}
else{
$tekst = null;
}
}
else {
global $$TplVar;
$tekst = $$TplVar;
}
$this->content = substr( $this->content, 0, $poz[1][1] - 1 ) . $tekst . substr( $this->content, $poz[1][2] );
} // end while
$this->content = substr( $this->content, 0 );
return true;
} // end function parse
function allParse( ){
if( isset( $this->fileContent[$this->TplFile] ) ){
$this->content = $this->fileContent[$this->TplFile];
}
else{
$this->content = $this->getContent( );
}
$this->parse( );
} // end function allParse
function blockParse( ){
if( isset( $this->blockContent[$this->TplFile][$this->block] ) ){
$this->content = $this->blockContent[$this->TplFile][$this->block];
}
else{
$this->content = $this->getFileBlock( );
if( isset( $this->content ) ){
if( $this->cache == true ){
$this->blockContent[$this->TplFile][$this->block] = $this->content;
}
}
}
$this->parse( );
} // end function blockParse
function getContent( $bBlock = null ){
if( isset( $this->fileContent[$this->TplFile] ) ){
$mReturn = $this->fileContent[$this->TplFile];
}
else{
if( isset( $bBlock ) ){
$mReturn = $this->getFile( $this->TplFile );
}
else{
$mReturn = $this->getFile( $this->TplFile );
}
if( $this->cache == true )
$this->fileContent[$this->TplFile] = $mReturn;
}
return $mReturn;
} // end function getContent
function getFile( $sFile ){
$rFile = fopen( $sFile, 'r' );
$iSize = filesize( $sFile );
if( $iSize > 0 )
$sContent = fread( $rFile, $iSize );
else
$sContent = null;
fclose( $rFile );
return ' '.$sContent;
} // end function getFile
function getFileBlock( $sFile = null, $sBlock = null ){
if( isset( $sFile ) && isset( $sBlock ) ){
$this->setFile( $sFile );
$this->setBlock( $sBlock );
}
$sFile = $this->getContent( true );
$iStart = strpos( $sFile, $this->startBlock.$this->block.$this->endBlockLine );
$iEnd = strpos( $sFile, $this->endBlock.$this->block.$this->endBlockLine );
if( is_int( $iStart ) && is_int( $iEnd ) ){
$iStart += strlen( $this->startBlock.$this->block.$this->endBlockLine );
return ' '.substr( $sFile, $iStart, $iEnd - $iStart );
}
else {
echo 'No block: <i>'.$this->block.'</i> in file: '.$this->TplFile.' <br />';
return null;
}
} // end function getFileBlock
function getFileArray( $sFile ){
return file( $sFile );
} // end function getFileArray
function getDir( ){
return $this->directory;
} // end function getDir
function setDir( $sDir ){
$this->directory = $sDir;
} // end function setDir
function setFile( $sFile ){
$this->TplFile = $sFile;
} // end function setFile
function setBlock( $sBlock ){
$this->block = $sBlock;
} // end function setBlock
}; // end class TplParser
?>