i'm using a template parser that i found on the internet
but it does not include the <!-- INCLUDE test --> option
can someone inplant one for me
i really need it
tnx in advance
- Code: Select all
<?php
/*******************************************************************
* template.class.php
* ------------------------------------------------
* date: 16-08-2005
* copyright: © 2005 Q-BB.nl
* author: Richard <richard@q-bb.nl>
*
* $ FileId: 16-08-2005_template.class.php_1.00_Richard $
*
*******************************************************************/
class template
{
// holds blocks and variables
// this is set up like this:
// $this->_data['block'][1]['VAR'] = $value;
var $_data = array ( );
// holds uncompiled code
var $code = array ( );
// the root directory, from which
// templates are coded
var $root_dir = '';
// Constructor: sets root directory
function template ( $root_dir = './style/default/' )
{
// the directory has to end with a slash
$root_dir .= ( substr ( $root_dir, -1 ) != '/' ) ? '/' : '';
// now check if it is a directory
if ( !is_dir ( $root_dir ) )
{
// if it is't a directory, return
return false;
}
// otherwise assign it..
$this->root_dir = $root_dir;
// .. and return
return true;
}
// set handles => files into our code array
function set_handle ( $handles )
{
// it needs to be an array, with at least one element
if ( !is_array ( $handles ) || count ( $handles ) == 0 )
{
// so if it's not, return false
return false;
}
// now loop through the array
foreach ( $handles as $handle => $file )
{
// the actual file
$file = $this->root_dir . $file;
// if it isn't a file..
if ( !is_file ( $file ) )
{
// .. return false
echo 'bad file name';
return false;
}
// add the filename
$this->code[$handle] = $file;
}
// return
return true;
}
// function for assigning variables
function assign_var ( $var, $value )
{
// just assign it;)
$this->_data[$var] = $value;
}
// use this for assigning multiple variables
// ( most used function )
function assign_vars ( $vars )
{
// it needs to be an array
if ( !is_array ( $vars ) )
{
// so return if it's not
return false;
}
// put them in the data array
$this->_data = array_merge ( $this->_data, $vars );
// and return
return true;
}
// assigning block variables
function assign_block_vars ( $blockname, $vars )
{
// check if it's a nested block
if ( strpos ( $blockname, '.' ) === false)
{
// if not, check if this one existed
if ( !isset ( $this->_data[$blockname . '.'] ) )
{
// if not, create
$this->_data[$blockname . '.'] = array ( );
}
// assign the variables
$this->_data[$blockname . '.'][] = $vars;
// and return
return true;
}
// otherwise, explode on dots
$blockname = explode ( '.', $blockname );
// begin code
$code = '$this->_data';
// loop through
for ( $i = 0, $size = count ( $blockname ) - 1; $i < $size; $i++ )
{
// add some more to the code
$code .= "['{$blockname[$i]}.']";
// evaluate a count
eval ( '$count = count ( ' . $code . ' ) - 1;' );
// add this count to the code
$code .= "[{$count}]";
}
// complete the code, evaluate
eval ( $code . "['{$blockname[$i]}.'][]=\$vars;" );
// return
return true;
}
// assigning one handle to one variable
function assign_var_from_handle ( $var, $handle )
{
// assign a parsed handle to a variable
return $this->assign_var ( $var, $this->parse ( $handle, false ) );
}
// destructor
function destroy ( $handle )
{
// just an unset;)
unset ( $this );
return true;
}
// parse handles
function parse ( $handle, $echo = true )
{
// check if it's set
if ( !isset ( $this->code[$handle] ) )
{
return false;
}
// compile code
$text = $this->compile ( implode ( '', file ( $this->code[$handle] ) ) );
// check if it needs to be echoed
if ( $echo === true )
{
// if so, echo;)
echo $text;
// return boolean true
return true;
}
// otherwise return the text
return $text;
}
// compilation method
function compile ( $text )
{
// replace all kinds of newlines to *nix newlines (\n)
$text = str_replace ( array ( "\r\n", "\r" ), "\n", $text );
// make the code php compatible
$text = str_replace ( '\\', '\\\\', $text );
$text = str_replace ( '\'', '\\\'', $text );
// replace variables with their php equal
$text = preg_replace ( '/{([a-z0-9._]*?)}/ie', '$this->generate_name ( "\1", true );', $text );
// split the text on newlines
$text = explode ( "\n", $text );
// preg_match array
$match = array ( );
// begin the return code (php code)
$return = '$return = \'';
// loop through all lines
for ( $i = 0, $size = count ( $text ); $i < $size; $i++ )
{
// if this line is the start of a block
if ( preg_match ( '/<!-- BEGIN ([a-z0-9._]*?) -->/i', $text[$i], $match ) )
{
// create valid php codes for this block
$name = $this->generate_name ( $match[1] );
// stop the php code for now
$return .= "';\n";
// create a variable for counting
$return .= "{$name['var']} = (isset ({$name['php']})) ? count ({$name['php']}) : 0;\n";
// start a for in the code
$return .= "for ({$name['i']} = 0; {$name['i']} < {$name['var']}; {$name['i']}++) {\n";
// begin the output code again
$return .= '$return .= \'';
// empty the match
$match = array ( );
}
// if this rule contains a block ender
elseif ( preg_match ( '/<!-- END (?:[a-z0-9._]*?) -->/i', $text[$i] ) )
{
// end this current code and start new code
$return .= '\'; } $return .= \'';
}
// if it doesn't have blocks, just add code
else
{
$return .= $text[$i] . "\n";
}
// memory saving
unset ( $text[$i] );
}
$return .= '\';';
// evaluate
if ( eval ( $return ) !== null )
{
echo $return;
}
return $return;
}
// function for creating php codes out of blocknames/variables
function generate_name ( $blockname, $variable = false )
{
// explode on dots
$blockname = explode ( '.', $blockname );
// count
$size = count ( $blockname );
// if it's a variable, count back
if ( $variable !== false && $size > 0 )
{
$size--;
}
// begin this code
$return = '$this->_data';
// the iteration count
$u = 'i';
// count down the size
$j = $size - 1;
// loop through
for ( $i = 0; $i < $size; $i++ )
{
// if it still needs an iteration count, loop through
if ( ( $variable === false && $i != $j ) || $variable === true )
{
// add the name, and an iteration count
$return .= "['{$blockname[$i]}.'][\${$u}]";
// update the iteration counter name
$u++;
}
// if it is complete
else
{
// add without the iteration
$return .= "['{$blockname[$i]}.']";
}
}
// if it's variable
if ( $variable !== false )
{
// latest block
$i = $blockname[$i];
// add it
$return .= "['{$i}']";
// the return value can be used in the php-template
$return = "' . ( ( isset ( {$return} ) ) ? {$return} : '' ) . '";
}
// if its a block
else
{
// return an array
$return = array (
// the code for the data
'php' => $return,
// the count variable
'var' => '$' . implode ( '_', $blockname ) . '_count_' . $u,
// the iteration count
'i' => '$' . $u
);
}
// return
return $return;
}
}
?>

