sTemplate = $sTemplate; $this->sTemplatePath = $sTemplatePath; $this->bDebug = $bDebug; } public function Set($sName, $vValue, $bStripHtml = TEMPLATE_STRIP_HTML, $bConvertEntities = TEMPLATE_CONVERT_ENTITIES, $sCharSet = TEMPLATE_ENCODING) { $this->aVars[$sName] = $vValue; // variable value might be a reference to a sub-template if (!($vValue instanceof Template) && is_scalar($vValue)) { if ($bStripHtml) { $this->aVars[$sName] = strip_tags($this->aVars[$sName]); } if ($bConvertEntities) { $this->aVars[$sName] = htmlentities($this->aVars[$sName], $sCharSet); } } } public function AddPostFilter($sFunctionName) { $this->aPostFilters[] = $sFunctionName; } public function Display() { $sOutput = ''; // looping rather than using extract because we need to determine the value type before assigning foreach ($this->aVars as $sKey => &$vValue) { // is this variable a reference to a sub-template if ($vValue instanceof Template) { // pass variables from parent to sub-template but don't override variables in sub-template if they already exist as they are more specific foreach ($this->aVars as $sSubKey => $vSubValue) { if (!($vSubValue instanceof Template) && !array_key_exists($sSubKey, $vValue->aVars)) { $vValue->aVars[$sSubKey] = $vSubValue; } } // display sub-template and assign output to parent variable $$sKey = $vValue->Display(); } else { $$sKey = $vValue; } } if ($this->bDebug) { $sOutput .= "\n\n"; } // use output buffers to capture data from require statement and store in variable ob_start(); require($this->sTemplatePath.$this->sTemplate); $sOutput .= ob_get_clean(); if ($this->bDebug) { $sOutput .= "\n\n"; } // process content against defined post filters foreach ($this->aPostFilters as $sPostFilter) { $sOutput = $sPostFilter($sOutput); } return $sOutput; } } ?>