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)
{
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 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;
BSRegister::Debug("Manually loading template '$name'");
$template = $this->_loadTemplate($name);
$template = $this->_parseTemplate($template);
}
if (!isset($this->usage["$name"]))
{
$this->usage["$name"] = 0;
}
$this->usage["$name"]++;
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))
{
trigger_error('There was no output to print');
exit;
}
if ($this->doneflush)
{
trigger_error('A template has already been sent to the output buffer');
exit;
}
$debugBlock = '';
if (BSRegister::GetDebug())
{
if (defined('SVN') AND preg_match('#^\$Id:?#', constant('SVN')))
{
$debugBlock .= preg_replace('#\$' . 'Id: (.+?) ([0-9].+?) [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}(.+?) (.+?) \$#', "\n
\n" . '
Query Debug |
";
$debugBlock .= "\n\t\t\t$query[query]\n\n\t\t\t ($query[time]) \n\n\t\t | \n\t