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; } // ################################################################### /** * 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) { trigger_error('You cannot cache templates more than once per initialization'); } else { $dbCache = array(); if ($this->dbCacheTable) { $db =& BSRegister::GetType('Db'); $cache = $db->query("SELECT * FROM {$this->dbCacheTable} WHERE filename IN ('" . implode("', '", $namearray) . "')"); while ($tpl = $db->fetchArray($cache)) { $time = filemtime(BSRegister::GetAppPath() . $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; $this->usage["$name"] = 0; } } } // ################################################################### /** * Loads a template from the file system from the specified * $templatedir with the file extension $extension * * @param string The name of the template call */ protected function _loadTemplate($name) { $path = BSRegister::GetAppPath() . $this->templateDir . $name . '.' . $this->extension; if (is_file($path)) { if (($template = @file_get_contents($path)) !== false) { return $template; } else { trigger_error("Could not load the template '$path'"); exit; } } else { trigger_error("Could not load the template '$path'"); exit; } } } /*=====================================================================*\ || ################################################################### || # $HeadURL$ || # $Id$ || ################################################################### \*=====================================================================*/ ?>