/'; var $global_replacements = array(); var $xml_replacements = array(); function Template($args = null) { $this->global_replacements['current_year'] = date('Y'); } // function show() // Takes either a 'template_file' argument or 'template', // containing the actual template data itself. function show($args) { if($args['template_file']) { $filename = $args['template_file']; if(file_exists($filename)) { $file_data = file_get_contents($filename); } else { // File DNE. //return false; trigger_error('Template File does not exist: "'.$filename.'"', E_USER_ERROR); } } elseif($args['template']) { $file_data = $args['template']; } else { trigger_error('No template passed to Template class', E_USER_ERROR); } $args['replacements'] = array_merge($this->global_replacements, $args['replacements']); if(is_array($args['replacements']) && count($args['replacements'])) { foreach($args['replacements'] as $key=>$value) { $file_data = str_replace('', $value, $file_data); } } // Before we can eval() html, we must remove any xml within it. $expression = '/<\\?xml(.*?)\\?>/s'; // The trailing "s" there lets the "." metachar accept newlines. $file_data = preg_replace_callback($expression, array(&$this, 'preg_catch_xml'), $file_data); // You can eval() html, but you have to fake out the parser by putting // a closing php tag before it. Adding a trailing opening tag will cause an error. ob_start(); eval('?>'.$file_data); $file_data = ob_get_contents(); ob_end_clean(); if(is_array($this->xml_replacements) && count($this->xml_replacements)) { foreach($this->xml_replacements as $key=>$value) { $file_data = str_replace($key, $value, $file_data); } } return $file_data; } function preg_catch_xml($matches) { $index_id = count($this->xml_replacements); $index_name = ''; $this->xml_replacements[$index_name] = $matches[0]; return $index_name; } } ?>