/** * The compile method. * * @params object HTML_Template_Flexy * @params string|false string to compile of false to use a file. * @return string filename of template * @access public */ function compile(&$flexy, $string = false) { // read the entire file into one variable // note this should be moved to new HTML_Template_Flexy_Token // and that can then manage all the tokens in one place.. global $_HTML_TEMPLATE_FLEXY_COMPILER; $gettextStrings =& $_HTML_TEMPLATE_FLEXY_COMPILER['gettextStrings']; $gettextStrings = array(); // reset it. if (@$this->options['debug']) { echo "compiling template {$flexy->currentTemplate}<BR>"; } // reset the elements. $flexy->_elements = array(); // replace this with a singleton?? $GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions'] = $this->options; $GLOBALS['_HTML_TEMPLATE_FLEXY']['elements'] = array(); $GLOBALS['_HTML_TEMPLATE_FLEXY']['filename'] = $flexy->currentTemplate; $GLOBALS['_HTML_TEMPLATE_FLEXY']['prefixOutput'] = ''; $GLOBALS['_HTML_TEMPLATE_FLEXY']['compiledTemplate'] = $flexy->compiledTemplate; if (is_array($this->options['Translation2'])) { require_once 'Translation2.php'; $this->options['Translation2'] = new Translation2($this->options['Translation2']['driver'], @$this->options['Translation2']['options']); } if ($this->is_a($this->options['Translation2'], 'Translation2')) { $this->options['Translation2']->setLang($this->options['locale']); // fixme - needs to be more specific to which template to use.. foreach ($this->options['templateDir'] as $tt) { $n = basename($flexy->currentTemplate); if (substr($flexy->currentTemplate, 0, strlen($tt)) == $tt) { $n = substr($flexy->currentTemplate, strlen($tt) + 1); } //echo $n; } $this->options['Translation2']->setPageID($n); } else { setlocale(LC_ALL, $this->options['locale']); } $data = $string; $res = false; if ($string === false) { $data = file_get_contents($flexy->currentTemplate); } // PRE PROCESS {_(.....)} translation markers. $got_gettext_markup = false; if (strpos($data, '{_(') !== false) { $matches = array(); $lmatches = explode('{_(', $data); array_shift($lmatches); // shift the first.. foreach ($lmatches as $k) { if (false === strpos($k, ')_}')) { continue; } $x = explode(')_}', $k); $matches[] = $x[0]; } //echo '<PRE>';print_r($matches); // we may need to do some house cleaning here... $gettextStrings = $matches; $got_gettext_markup = true; // replace them now.. // ** leaving in the tag (which should be ignored by the parser.. // we then get rid of the tags during the toString method in this class. foreach ($matches as $v) { $data = str_replace('{_(' . $v . ')_}', '{_(' . $this->translateString($v) . ')_}', $data); } } if (isset($_HTML_TEMPLATE_FLEXY_COMPILER['cache'][md5($data)])) { $res = $_HTML_TEMPLATE_FLEXY_COMPILER['cache'][md5($data)]; } else { $tokenizer = new HTML_Template_Flexy_Tokenizer($data); $tokenizer->fileName = $flexy->currentTemplate; //$tokenizer->debug=1; $tokenizer->options['ignore_html'] = $this->options['nonHTML']; $tokenizer->options['ignore_php'] = !$this->options['allowPHP']; $res = HTML_Template_Flexy_Token::buildTokens($tokenizer); $_HTML_TEMPLATE_FLEXY_COMPILER['cache'][md5($data)] = $res; } if ($this->is_a($res, 'PEAR_Error')) { return $res; } // turn tokens into Template.. $data = $res->compile($this); if ($this->is_a($data, 'PEAR_Error')) { return $data; } $data = $GLOBALS['_HTML_TEMPLATE_FLEXY']['prefixOutput'] . $data; if (@$this->options['debug']) { echo "<B>Result: </B><PRE>" . htmlspecialchars($data) . "</PRE><BR>"; } if ($this->options['nonHTML']) { $data = str_replace("?>\n", "?>\n\n", $data); } // at this point we are into writing stuff... if ($this->options['compileToString']) { $flexy->elements = $GLOBALS['_HTML_TEMPLATE_FLEXY']['elements']; return $data; } // error checking? $file = $flexy->compiledTemplate; if (isset($flexy->options['output.block'])) { list($file, $part) = explode('#', $file); } if ($cfp = fopen($file, 'w')) { if (@$this->options['debug']) { echo "<B>Writing: </B>" . htmlspecialchars($data) . "<BR>"; } fwrite($cfp, $data); fclose($cfp); chmod($file, 0775); // make the timestamp of the two items match. clearstatcache(); touch($file, filemtime($flexy->currentTemplate)); if ($file != $flexy->compiledTemplate) { chmod($flexy->compiledTemplate, 0775); // make the timestamp of the two items match. clearstatcache(); touch($flexy->compiledTemplate, filemtime($flexy->currentTemplate)); } } else { return HTML_Template_Flexy::raiseError('HTML_Template_Flexy::failed to write to ' . $flexy->compiledTemplate, HTML_TEMPLATE_FLEXY_ERROR_FILE, HTML_TEMPLATE_FLEXY_ERROR_RETURN); } // gettext strings if (file_exists($flexy->getTextStringsFile)) { unlink($flexy->getTextStringsFile); } if ($gettextStrings && ($cfp = fopen($flexy->getTextStringsFile, 'w'))) { fwrite($cfp, serialize(array_unique($gettextStrings))); fclose($cfp); } // elements if (file_exists($flexy->elementsFile)) { unlink($flexy->elementsFile); } if ($GLOBALS['_HTML_TEMPLATE_FLEXY']['elements'] && ($cfp = fopen($flexy->elementsFile, 'w'))) { fwrite($cfp, serialize($GLOBALS['_HTML_TEMPLATE_FLEXY']['elements'])); fclose($cfp); // now clear it. } return true; }
/** * The compile method. * * @params object HTML_Template_Flexy * @params string|false string to compile of false to use a file. * @return string filename of template * @access public */ function compile($flexy, $string = false) { // read the entire file into one variable // note this should be moved to new HTML_Template_Flexy_Token // and that can then manage all the tokens in one place.. global $_HTML_TEMPLATE_FLEXY_COMPILER; $this->flexy = $flexy; $this->currentTemplate = $flexy->currentTemplate; $gettextStrings =& $_HTML_TEMPLATE_FLEXY_COMPILER['gettextStrings']; $gettextStrings = array(); // reset it. if (@$this->options['debug']) { echo "compiling template {$flexy->currentTemplate}<BR>"; } // reset the elements. $flexy->_elements = array(); // replace this with a singleton?? $GLOBALS['_HTML_TEMPLATE_FLEXY']['currentOptions'] = $this->options; $GLOBALS['_HTML_TEMPLATE_FLEXY']['elements'] = array(); $GLOBALS['_HTML_TEMPLATE_FLEXY']['filename'] = $flexy->currentTemplate; $GLOBALS['_HTML_TEMPLATE_FLEXY']['prefixOutput'] = ''; $GLOBALS['_HTML_TEMPLATE_FLEXY']['compiledTemplate'] = $flexy->compiledTemplate; // initialize Translation 2, and $this->flexy->initializeTranslator(); // load the template! $data = $string; $res = false; if ($string === false) { $data = file_get_contents($flexy->currentTemplate); } // PRE PROCESS {_(.....)} translation markers. if (strpos($data, '{_(') !== false) { $data = $this->preProcessTranslation($data); } // Tree generation!!! if (!$this->options['forceCompile'] && isset($_HTML_TEMPLATE_FLEXY_COMPILER['cache'][md5($data)])) { $res = $_HTML_TEMPLATE_FLEXY_COMPILER['cache'][md5($data)]; } else { $tokenizer = new HTML_Template_Flexy_Tokenizer($data); $tokenizer->fileName = $flexy->currentTemplate; //$tokenizer->debug=1; $tokenizer->options['ignore_html'] = $this->options['nonHTML']; require_once 'HTML/Template/Flexy/Token.php'; $res = HTML_Template_Flexy_Token::buildTokens($tokenizer); if ($this->is_a($res, 'PEAR_Error')) { return $res; } $_HTML_TEMPLATE_FLEXY_COMPILER['cache'][md5($data)] = $res; } // technically we shouldnt get here as we dont cache errors.. if ($this->is_a($res, 'PEAR_Error')) { return $res; } // turn tokens into Template.. $data = $res->compile($this); if ($this->is_a($data, 'PEAR_Error')) { return $data; } $data = $GLOBALS['_HTML_TEMPLATE_FLEXY']['prefixOutput'] . $data; if ($flexy->options['debug'] > 1) { echo "<B>Result: </B><PRE>" . htmlspecialchars($data) . "</PRE><BR>\n"; } if ($this->options['nonHTML']) { $data = str_replace("?>\n", "?>\n\n", $data); } // at this point we are into writing stuff... if ($flexy->options['compileToString']) { if ($flexy->options['debug']) { echo "<B>Returning string:<BR>\n"; } $flexy->elements = $GLOBALS['_HTML_TEMPLATE_FLEXY']['elements']; return $data; } // error checking? $file = $flexy->compiledTemplate; if (isset($flexy->options['output.block'])) { list($file, $part) = explode('#', $file); } if ($cfp = fopen($file, 'w')) { if ($flexy->options['debug']) { echo "<B>Writing: </B>{$file}<BR>\n"; } fwrite($cfp, $data); fclose($cfp); chmod($file, 0775); // make the timestamp of the two items match. clearstatcache(); touch($file, filemtime($flexy->currentTemplate)); if ($file != $flexy->compiledTemplate) { chmod($flexy->compiledTemplate, 0775); // make the timestamp of the two items match. clearstatcache(); touch($flexy->compiledTemplate, filemtime($flexy->currentTemplate)); } } else { return HTML_Template_Flexy::staticRaiseError('HTML_Template_Flexy::failed to write to ' . $flexy->compiledTemplate, HTML_TEMPLATE_FLEXY_ERROR_FILE, HTML_TEMPLATE_FLEXY_ERROR_RETURN); } // gettext strings if (file_exists($flexy->getTextStringsFile)) { unlink($flexy->getTextStringsFile); } if ($gettextStrings && ($cfp = fopen($flexy->getTextStringsFile, 'w'))) { fwrite($cfp, serialize(array_unique($gettextStrings))); fclose($cfp); chmod($flexy->getTextStringsFile, 0664); } // elements if (file_exists($flexy->elementsFile)) { unlink($flexy->elementsFile); } if ($GLOBALS['_HTML_TEMPLATE_FLEXY']['elements'] && ($cfp = fopen($flexy->elementsFile, 'w'))) { fwrite($cfp, serialize($GLOBALS['_HTML_TEMPLATE_FLEXY']['elements'])); fclose($cfp); chmod($flexy->elementsFile, 0664); // now clear it. } return true; }