rendered paste body<?php/** * Template System * File: template.php */ //error_reporting( E_ALL | E_STRICT );class Tpl { var $output; /** * Loads a template file * * @param String $templateFile * */ public function loadTemplate( $templateFile ) { if (!file_exists(THEME_PATH.$templateFile.".tpl")) { die("Could not find file '" . THEME_PATH.$templateFile . ".tpl'"); } $this->output .= file_get_contents(THEME_PATH.$templateFile.".tpl"); } /** * Include a template file * * @param String $templateFile * */ public function incTemplate( $templateFile ) { if (!file_exists(THEME_PATH.$templateFile.".tpl")) { die("Could not find file '" . THEME_PATH.$templateFile . ".tpl'"); } return file_get_contents(THEME_PATH.$templateFile.".tpl"); } /** * Parse's the data into the template * * @param String $templateFile * @param Array $vars * @return print $this->output */ public function parse( $templateFile, $vars=array() ) { // Include the main template file being used, as defined by $templateFile $this->loadTemplate($templateFile); /** * Handles the inclusion of template files * Usage: <!-- INCLUDE 'filename' --> */ $matches = array(); $pattern = "#<!-- INCLUDE (.*?) -->#"; $amount = preg_match_all( $pattern, $this->output, $matches ); for( $i = 0; $i < $amount; $i++ ) { $name = trim( $matches[1][$i] ); $file = $this->incTemplate( $name ); $this->output = str_replace( "<!-- INCLUDE {$name} -->", $file, $this->output ); } unset( $matches ); /** * Parse settings into the template when required * Usage: {Base::'Name'} */ $matches = array(); $pattern = "#{Base::(.*?)}#"; $amount = preg_match_all( $pattern, $this->output, $matches ); for( $i = 0; $i < $amount; $i++ ) { $name = trim( $matches[1][$i] ); $setting = Base::setting( $name ); $this->output = str_replace( "{Base::{$name}}", $setting, $this->output ); } unset( $matches ); /** * Parse settings into the template when required * Usage: {Theme::'Name'} */ $matches = array(); $pattern = "#{Theme::(.*?)}#"; $amount = preg_match_all( $pattern, $this->output, $matches ); for( $i = 0; $i < $amount; $i++ ) { $name = trim( $matches[1][$i] ); $setting = Base::theme( $name ); $this->output = str_replace( "{Theme::{$name}}", $setting, $this->output ); } unset( $matches ); /** * Parse variables in the array * Usage: {'name'} */ if( is_array( $vars ) ){ foreach($vars as $var => $data){ if ( ! is_array($data)) { $this->output = str_replace('{'.$var.'}',$data,$this->output); } else { $this->output = $this->parseArray($var, $data, $this->output); } } } print $this->output; } /** * Parse blocks (arrays inside the main array) * * @param String $vars * @param String $data * @return $output */ public function parseArray($vars, $data, $output) { if (FALSE == ($match = $this->matchPair($output, $vars))) { return $output; } $str = ''; foreach ($data as $row) { $temp = $match['1']; foreach ($row as $var => $data) { if (!is_array($var)) { $temp = str_replace('{'.$var.'}',$data,$temp); } else { $temp = $this->parseArray($var, $data, $temp); } } $str .= $temp; } return str_replace($match['0'], $str, $output); } /** * Match pair's that are used for blocks * * @param String $var * @return $output */ public function matchPair($output, $var) { if ( ! preg_match("|{" . $var . "}(.+){". '/' . $var . "}|s", $output, $match)) { return FALSE; } return $match; }}$tpl = &new Tpl();?>