public static $globalVars = array();
/**
- * The file name of the template
+ * The path of the template file
* @var string
*/
- protected $filename;
+ protected $path;
+
+ /**
+ * The name of the template
+ * @var string
+ */
+ protected $name;
/**
* Template contents
return; // there's no point in pre-caching file templates
}
+ $namearray = array_map(array('self', '_path'), $namearray);
$cache = BSApp::$db->query("SELECT * FROM " . self::$dbCacheTable . " WHERE filename IN ('" . implode("', '", $namearray) . "')");
while ($tpl = $cache->fetchArray())
{
*
* @param string File name
*/
- public function __construct($path)
+ public function __construct($name)
{
- $this->file = $path;
+ $this->name = $name;
+ $this->path = self::_path($name);
// checks to see if the template has been cached
- if (isset(self::$cache[$this->file]))
+ if (isset(self::$cache[$this->path]))
{
- if (!self::$dbCacheTable || filemtime(sprintf(self::$templatePath, $this->file)) <= self::$cache[$this->file]['timestamp'])
+ if (!self::$dbCacheTable || filemtime($this->path) <= self::$cache[$this->path]['timestamp'])
{
- $this->template = self::$cache[$this->file]['template'];
+ $this->template = self::$cache[$this->path]['template'];
return;
}
}
// it hasn't been cached
- $path = sprintf(self::$templatePath, $this->file);
- if (!is_file($path) || !is_readable($path))
+ if (!is_file($this->path) || !is_readable($this->path))
{
- throw new Exception("Could not load the template $path");
+ throw new Exception("Could not load the template {$this->path}");
}
- $this->template = $this->_parseTemplate(file_get_contents($path));
- self::$cache[$this->file]['template'] = $this->template;
+ $this->template = $this->_parseTemplate(file_get_contents($this->path));
+ self::$cache[$this->path]['template'] = $this->template;
// store the template in the database
if (self::$dbCacheTable)
{
- BSApp::$db->query("REPLACE INTO " . self::$dbCacheTable . " SET template = '" . BSApp::$db->escapeString($this->template) . "', timestamp = " . TIMENOW . ", filename = '" . $this->file . "'");
- self::$cache[$this->file]['time'] = TIMENOW;
+ BSApp::$db->query("REPLACE INTO " . self::$dbCacheTable . " SET template = '" . BSApp::$db->escapeString($this->template) . "', timestamp = " . TIMENOW . ", filename = '" . $this->path . "'");
+ self::$cache[$this->path]['time'] = TIMENOW;
}
}
+ /**
+ * Evaluates and returns the template. This is equivalent to calling:
+ * $tpl->evaluate()->getTemplate()
+ *
+ * @return string
+ */
+ public function __toString()
+ {
+ return $this->evaluate()->getTemplate();
+ }
+
/**
* Returns the template data
*
throw new Exception('There is no output to print');
}
- $template = $this->template;
+ echo $this->template;
+ }
+
+ /**
+ * Returns the debug block
+ *
+ * @return string
+ */
+ public static function get_debug_block()
+ {
+ if (!BSApp::get_debug())
+ {
+ return;
+ }
- $debugBlock = '';
- if (BSApp::get_debug() && strpos($template, '</body>') !== false)
+ $debugBlock = "\n<div align=\"center\">Executed in " . round(BSFunctions::fetch_microtime_diff('0 ' . $_SERVER['REQUEST_TIME']), 10) . ' seconds</div>';
+ $debugBlock .= "\n<br /><div align=\"center\">" . BSApp::get_debug_list() . "</div>";
+
+ if (BSApp::$db)
{
- $debugBlock .= "\n<div align=\"center\">Executed in " . round(BSFunctions::fetch_microtime_diff('0 ' . $_SERVER['REQUEST_TIME']), 10) . ' seconds</div>';
- $debugBlock .= "\n<br /><div align=\"center\">" . BSApp::get_debug_list() . "</div>";
-
- if (BSApp::$db)
+ $queries = BSApp::$db->getHistory();
+
+ $debugBlock .= "<br />\n" . '<table cellpadding="4" cellspacing="1" border="0" align="center" width="30%" style="background-color: rgb(60, 60, 60); color: white">' . "\n\t" . '<tr><td><strong>Query Debug</strong></td></tr>';
+
+ foreach ($queries as $query)
{
- $queries = BSApp::$db->getHistory();
-
- $debugBlock .= "<br />\n" . '<table cellpadding="4" cellspacing="1" border="0" align="center" width="30%" style="background-color: rgb(60, 60, 60); color: white">' . "\n\t" . '<tr><td><strong>Query Debug</strong></td></tr>';
-
- foreach ($queries as $query)
- {
- $debugBlock .= "\n\t<tr style=\"background-color: rgb(230, 230, 230); color: black\">";
- $debugBlock .= "\n\t\t<td>";
- $debugBlock .= "\n\t\t\t$query[query]\n\n\t\t\t<div style=\"font-size: 9px;\">($query[time])</div>\n<!--\n$query[trace]\n-->\n\t\t</td>\n\t</tr>";
- }
-
- $debugBlock .= "\n</table>\n\n\n";
+ $debugBlock .= "\n\t<tr style=\"background-color: rgb(230, 230, 230); color: black\">";
+ $debugBlock .= "\n\t\t<td>";
+ $debugBlock .= "\n\t\t\t$query[query]\n\n\t\t\t<div style=\"font-size: 9px;\">($query[time])</div>\n<!--\n$query[trace]\n-->\n\t\t</td>\n\t</tr>";
}
- $template = str_replace('</body>', $debugBlock . '</body>', $template);
+ $debugBlock .= "\n</table>\n\n\n";
}
-
- print($template);
+
+ return $debugBlock;
}
/**
return $template;
}
+
+ /**
+ * Returns the full path of a template given a name
+ *
+ * @param string Template name
+ *
+ * @return string Template path
+ */
+ protected static function _path($name)
+ {
+ return sprintf(self::$templatePath, $name);
+ }
}
?>
\ No newline at end of file