* @package ISSO
*/
+require_once('ISSO/Functions.php');
+
/**
-* Database-Driven Template System
+* File-Based Template System
+*
+* This framework merely replaces the template loading functions with
+* file-system based ones. It has an optional caching system in which
+* template data will remain stored in the database as long as the filesystem
+* file is modified. To do this, pass a table name to setDatabaseCache() and make sure
+* there's a DB module that has access to a table with this schema:
*
-* This framework is a backend to the database template engine and
-* contains all the parsing algorithms.
+* CREATE TABLE template (filename VARCHAR (250) NOT NULL, template TEXT NOT NULL, timestamp INT NOT NULL);
*
* @author Blue Static
* @copyright Copyright ©2002 - [#]year[#], Blue Static
class BSTemplate
{
/**
- * The database object to use
- * @var object
- */
- private $db;
-
- /**
- * Name of the database table templates are in
- * @var string
- */
- private $tableName = '';
-
- /**
- * Name of the table column template names are in
+ * The path, from the path of the application, where templates are stored
* @var string
*/
- private $nameColumn = '';
+ private $templateDir = '';
/**
- * Name of the table column templates are in
+ * The extension all the template files have
* @var string
*/
- private $dataColumn = '';
+ private $extension = 'tpl';
/**
- * Additional WHERE clauses for the template fetch SQL
- * @var string
+ * The database table name for the template cache
+ * @var string
*/
- private $extraWhere = '';
+ private $dbCacheTable = null;
/**
* The name of the function phrases are fetched with
// ###################################################################
/**
- * Constructor: check required modules
+ * Sets the template directory name
+ *
+ * @param string Template directory name
*/
- public function __construct()
+ public function setTemplateDirectory($dir)
{
- BSRegister::RequiredModules(array('Db'));
+ $this->templateDir = BSFunctions::FetchSourcePath($dir);
}
// ###################################################################
/**
- * Sets the database object to use
+ * Sets the file extension for the templates
*
- * @param object Database object
+ * @param string File extension
*/
- public function setDatabase($db)
+ public function setExtension($ext)
{
- $this->db = $db;
+ $this->extension = $ext;
}
// ###################################################################
/**
- * Sets the table name and column information
+ * Sets the name of the table to access for the datbase cache
*
- * @param string The database table name
- * @param string Name of the column that stores the name of the template
- * @param string Name of the column that stores template data
+ * @param string DB table name
*/
- public function setDatabaseTable($tableName, $nameColumn, $dataColumn)
+ public function setDatabaseCache($table)
{
- $this->tableName = $tableName;
- $this->nameColumn = $nameColumn;
- $this->dataColumn = $dataColumn;
+ $this->dbCacheTable = $table;
}
// ###################################################################
}
else
{
- $templates = $this->db->query("SELECT * FROM " . $this->tableName . " WHERE " . $this->nameColumn . " IN ('" . implode("', '", $namearray) . "')" . ($this->extrawhere ? ' ' . $this->extrawhere : ''));
- while ($template = $this->db->fetch_array($templates))
+ $dbCache = array();
+ if ($this->dbCacheTable)
{
- $this->cache[ $template[ $this->nameColumn ] ] = $this->_parse($template[ $this->dataColumn ]);
- $this->usage[ $template[ $this->nameColumn ] ] = 0;
+ $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;
}
}
}
*/
protected function _loadTemplate($name)
{
- if ($template = $this->db->queryFirst("SELECT * FROM " . $this->tableName . " WHERE " . $this->nameColumn . " = '$name'" . ($this->extrawhere ? ' ' . $this->extrawhere : '')))
+ $path = BSRegister::GetAppPath() . $this->templateDir . $name . '.' . $this->extension;
+ if (is_file($path))
{
- return $template[ $this->dataColumn ];
+ if (($template = @file_get_contents($path)) !== false)
+ {
+ return $template;
+ }
+ else
+ {
+ trigger_error("Could not load the template '$path'");
+ exit;
+ }
}
else
{
- trigger_error("The template '$name' could not be loaded");
+ trigger_error("Could not load the template '$path'");
exit;
}
}
+++ /dev/null
-<?php
-/*=====================================================================*\
-|| ###################################################################
-|| # Blue Static ISSO Framework
-|| # Copyright ©2002-[#]year[#] Blue Static
-|| #
-|| # This program is free software; you can redistribute it and/or modify
-|| # it under the terms of the GNU General Public License as published by
-|| # the Free Software Foundation; version [#]gpl[#] of the License.
-|| #
-|| # This program is distributed in the hope that it will be useful, but
-|| # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
-|| # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
-|| # more details.
-|| #
-|| # You should have received a copy of the GNU General Public License along
-|| # with this program; if not, write to the Free Software Foundation, Inc.,
-|| # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
-|| ###################################################################
-\*=====================================================================*/
-
-/**
-* File System Template System (TemplateFs.php)
-*
-* @package ISSO
-*/
-
-require_once('ISSO/Template.php');
-require_once('ISSO/Functions.php');
-
-/**
-* File System Template System
-*
-* This framework merely replaces the template loading functions with
-* file-system based ones. It has an optional caching system in which
-* template data will remain stored in the database as long as the filesystem
-* file is modified. To do this, pass a table name to setDatabaseCache() and make sure
-* there's a DB module that has access to a table with this schema:
-*
-* CREATE TABLE template (filename VARCHAR (250) NOT NULL, template TEXT NOT NULL, timestamp INT NOT NULL);
-*
-* @author Blue Static
-* @copyright Copyright ©2002 - [#]year[#], Blue Static
-* @version $Revision$
-* @package ISSO
-*
-*/
-class BSTemplateFs extends BSTemplate
-{
- /**
- * The path, from the path of the application, where templates are stored
- * @var string
- */
- private $templateDir = '';
-
- /**
- * The extension all the template files have
- * @var string
- */
- private $extension = 'tpl';
-
- /**
- * The database table name for the template cache
- * @var string
- */
- private $dbCacheTable = null;
-
- // ###################################################################
- /**
- * Constructor (overriding so we don't require the Db module)
- */
- public function __construct() {}
-
- // ###################################################################
- /**
- * Sets the template directory name
- *
- * @param string Template directory name
- */
- public function setTemplateDirectory($dir)
- {
- $this->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$
-|| ###################################################################
-\*=====================================================================*/
-?>
\ No newline at end of file