templateDir = BSFunctions::FetchSourcePath($dir); } // ################################################################### /** * Sets the file extension for the templates * * @param string File extension */ public function setExtension($ext) { $this->extension = $ext; } // ################################################################### /** * Sets the name of the table to access for the datbase cache * * @param string DB table name */ public function setDatabaseCache($table) { $this->dbCacheTable = $table; } // ################################################################### /** * Sets the pre-parse hook method which is called before any other * processing is done on the template. * * @param string Method name */ public function setPreParseHook($hook) { $this->preParseHook = $hook; } // ################################################################### /** * Takes an array of template names, loads them, and then stores a * parsed version for optimum speed. * * @param array List of template names to be cached */ public function cache($namearray) { if (sizeof($this->cache) > 0) { throw new Exception('You cannot cache templates more than once per initialization'); } else { $dbCache = array(); if ($this->dbCacheTable) { $db =& BSApp::Registry()->getType('Db'); $cache = $db->query("SELECT * FROM {$this->dbCacheTable} WHERE filename IN ('" . implode("', '", $namearray) . "')"); while ($tpl = $cache->fetchArray()) { $time = filemtime($this->templateDir . $tpl['filename'] . '.' . $this->extension); $template = $tpl['template']; if ($time > $tpl['timestamp']) { $template = $this->_parseTemplate($this->_loadTemplate($tpl['filename'])); $db->query("UPDATE {$this->dbCacheTable} SET template = '" . $db->escapeString($template) . "', timestamp = " . TIMENOW . " WHERE filename = '" . $tpl['filename'] . "'"); $tpl['template'] = $template; } $dbCache["$tpl[filename]"] = $template; } } foreach ($namearray AS $name) { if ($this->dbCacheTable) { if (isset($dbCache["$name"])) { $template = $dbCache["$name"]; } else { $template = $this->_parseTemplate($this->_loadTemplate($name)); $db->query("INSERT INTO {$this->dbCacheTable} (filename, template, timestamp) VALUES ('$name', '" . $db->escapeString($template) . "', " . TIMENOW . ")"); } } else { $template = $this->_parseTemplate($this->_loadTemplate($name)); } $this->cache[$name] = $template; } } } // ################################################################### /** * Loads a template from the cache or the _load function and stores the * parsed version of it * * @param string The name of the template * * @return string A parsed and loaded template */ public function fetch($name) { if (isset($this->cache[$name])) { $template = $this->cache[$name]; } else { $this->uncached[$name]++; BSApp::Debug("Manually loading template '$name'"); $template = $this->_loadTemplate($name); $template = $this->_parseTemplate($template); } return $template; } // ################################################################### /** * Output a template fully compiled to the browser * * @param string Compiled and ready template */ public function flush($template) { ob_start(); if (empty($template)) { throw new Exception('There was no output to print'); } if ($this->doneflush) { throw new Exception('A template has already been sent to the output buffer'); } $debugBlock = ''; if (BSApp::GetDebug()) { $debugBlock .= "\n
Query Debug |
";
$debugBlock .= "\n\t\t\t$query[query]\n\n\t\t\t ($query[time]) \n\n\t\t | \n\t