files = array(); $this->subtemplates = array(); $this->localization = $localization; } /** * Transforms a sub-template to the HTML output. * @param $filename File containing the used sub-template. * @param $subtemplate Name of the subtempalte to use. * @return string HTML code ready to be displayed. */ public function GetHTMLCode($filename, $subtemplate) { $this->ParseFile($filename); // Reads the sub-template return $this->Evaluate($filename, $subtemplate); // Replaces all HTML variables. } /** * Reads the file and extracts all sub-templates. * @param $filename File to parse. * @return void */ private function ParseFile($filename) { // If the file has already be parsed, we do not parse it again. if($this->files[$filename] !== true) { // Retrieving file content. $filename = addslashes($filename); if(!file_exists($filename)) throw new Exception('File '.$filename.' does not exist.'); $this->files[$filename] = file_get_contents($filename); // Retrieving all sub-templates from file. $this->GetSubTemplates($filename); } } /** * Retrieving all sub-templates from a given file. * @param $filename File from which to extract sub-templates. * @return void */ private function GetSubTemplates($filename) { /** * All sub-templates will look like the following: * * * <h1>This is my sub-templates</h1> * * No closing tags are required. */ // Splitting the file in sub-templates. $explosion = explode('', $tmp); $subTemplateName = $tmp[0]; $subTemplateContent = $tmp[1]; // If internationalized, we translate it now with gettext extension. $this->subtemplates[$filename][$subTemplateName] = ($this->localization ? $this->Localize($subTemplateContent) : $subTemplateContent); } // If the file is parsed, we replace the textual content heavy in memory by a single bit. $this->files[$filename] = true; } /** * Replaces all variables by their content in the HTML file. * @param $filename File where to replace the variables. * @param $subtemplate Sub-template to use. * @return string HTML code ready to be displayed. */ private function Evaluate($filename, $subtemplate) { /** * All variables will be written in the HTML code with ${VARIABLE_NAME}. */ // Retrieving the localized (or not) HTML content, without replacing variables. $evaluatedCode = $this->subtemplates[$filename][$subtemplate]; // Retrieving all variables names from the sub-template. preg_match_all('#\${(.*)}#U', $evaluatedCode, $variables); $variables = $variables[1]; // Replacing the value of each variable. foreach ($variables as $variable) { global $$variable; $evaluatedCode = str_replace('${'.$variable.'}', $$variable, $evaluatedCode); } // Returns the code with variable values inside. return $evaluatedCode; } /** * Translates the content of a sub-template thanks to gettext PHP extension. * @param $content Content of the sub-template to translate. * @return string Translated content. */ private function Localize($content) { /** * All texts to internationalize will be written like the following: _("Text to translate."). */ // Retrieving text to internationalize. preg_match_all('#_\(\"(.+)\"\)#Us', $content, $unlocalizedTexts); $unlocalizedTexts = $unlocalizedTexts[1]; // Translating all pieces of text. foreach($unlocalizedTexts as $unlocalizedText) { $content = preg_replace('#_\(\"'.$unlocalizedText.'\"\)#Us', gettext($unlocalizedText), $content); } // Returns the translated text. return $content; } /** * Writes a buffer, results of some templates engine treatments. Static function for better performances. * @param $buffer Text to write. * @return void */ public static function Output($buffer) { // You will be able to use here some display functions depending your configuration, as the following: // $buffer = utf8_encode($buffer); // $buffer = strip_slashes($buffer); echo $buffer; } /** * Used to print the content of our template engine. For debug purposes. * @return void */ public function Debug() { echo '
';
			print_r($this);
			die('
'); } } ?>