Templating Class Troubles

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Templating Class Troubles

Post by LiveFree »

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:

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! */
?>
The useage is as follows

Code: Select all

(initialize)
$template->assign(array("header.htm"));
$template->assignVar("header", array("VAR NAME" => "REAL TEXT HERE"));
$template->parse("header");
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 :)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

<?php
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
}
?>
To test this, can you echo out the value for $this->clean[$handle]? The problem is that you are passing something to the eval() function that eval() doesn't like. Echoing out the value of $this->clean[$handle] might show you what is being passed that is choking your app.
Post Reply