Templating Class Troubles
Posted: Sat Jun 03, 2006 8:08 pm
Hey All,
Well with inspiration from a tutorial I have written my own templating class, but when I try to use it... I get these errors:
Parse error: parse error, unexpected $, expecting '(' ...... in eval()'ed code
And the class is as follows:
The useage is as follows
The header handle is handled with the explode(".") to get the filename w/o the extension
If anyone can spot the error, that'd be great
Well with inspiration from a tutorial I have written my own templating class, but when I try to use it... I get these errors:
Parse error: parse error, unexpected $, expecting '(' ...... in eval()'ed code
And the class is as follows:
Code: Select all
<?php
class template
{
var $root = './pages/';
var $html = array();
var $plain = array();
var $clean = array();
function template()
{
/*
Makes sure root directory is valid
*/
if ( !is_dir($this->root) ){
die('Critical Error: The Directory '. $this->root . ' does not exist!');
}
}
function assign($handle_array)
{
if ( !is_array($handle_array) ){
die('Critical Error: Invalid parameter passed for assign()!');
}
foreach ($handle_array as $filename) {
$foo = explode(".", $filename);
$name = $foo[0];
echo $name;
$fileref = $this->root . $filename;
// Load all data from that file into an array
$this->html[$name] = implode("", file($fileref));
}
}
function assignVar($handle, $var_arr)
{
if ( !is_array($var_arr) ) {
die('Critical Error: Invalid parameter passed for assignVar()!');
}
if ( !isset($handle) ){
die('Critical Error: File handle is not set!');
}
if ( !empty($this->html[$handle]) ) {
/*
The file handle is good to go and we can begin replacing
*/
foreach ($var_arr as $ref=>$replace){
$this->dirty['{'. $ref . '}'] = $replace;
}
} else {
die('Critical Error: Could not get style data!');
}
}
function parse($handle)
{
if ( !isset($handle) || empty($this->html[$handle]) ){
die('Critical Error: Invalid parameter passed for parse()!');
}
$this->clean[$handle] = $this->compile($handle);
eval($this->clean[$handle]);
// And...thats all she wrote folks
// The updated info is now outputted to the browser
}
function compile($handle)
{
if ( empty($this->html[$handle]) || empty($handle) ){
die('Critical Error: Data can not be compiled!');
}
$data = $this->html[$handle]; // Start processing the data now
$data = str_replace('\\', '\\\\', $data);
$data = str_replace('\'', '\\\'', $data);
$code = explode("\n", $code);
$lines = sizeof($code);
for( $n = 0;$n < $lines;$n++ )
{
foreach( $this->dirty as $ref=>$replace )
{
if( preg_match( "/^(.*)" . $ref . "(.*)$/i", $code[$n] ) )
{
$code[$n] = preg_replace( "/^(.*)" . $ref . "(.*)$/", "\\1" . $replace . "\\2", $code[$n] );
}
}
$code[$n] = 'echo \'' . $code[$n] . '\' . "\\n";';
}
$code = explode("\n", $code);
return $code;
}
}
/* Thanks to 'The Kinder' of PHPFreaks for being the inspiration of this! */
?>Code: Select all
(initialize)
$template->assign(array("header.htm"));
$template->assignVar("header", array("VAR NAME" => "REAL TEXT HERE"));
$template->parse("header");If anyone can spot the error, that'd be great