load('template', null); /** * 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 Template_FS extends Template { /** * The path, from the path of the application, where templates are stored * @var string * @access private */ var $templatedir = ''; /** * The extension all the template files have * @var string * @access private */ var $extension = 'tpl'; /** * The database table name for the template cache * @var string * @access private */ var $dbCacheTable = null; // ################################################################### /** * Constructor */ function __construct(&$registry) { $this->registry =& $registry; } // ################################################################### /** * (PHP 4) Constructor */ function Template_FS(&$registry) { $this->__construct($registry); } // ################################################################### /** * Sets the template directory * * @access public * * @param string The template directory */ function setTemplateDir($path) { $this->templatedir = $this->registry->fetch_sourcepath($path); } // ################################################################### /** * Sets the file extension * * @access public * * @param string File extension */ function setExtension($ext) { $this->extension = $ext; } // ################################################################### /** * Sets the name of the table to access for the datbase cache * * @param string DB table name */ function setDatabaseCache($table) { $this->dbCacheTable = $table; } // ################################################################### /** * Takes an array of template names, loads them, and then stores a * parsed version for optimum speed. * * @access public * * @param array List of template names to be cached */ function cache($namearray) { if (sizeof($this->cache) > 0) { trigger_error('You cannot cache templates more than once per initialization', E_USER_WARNING); } else { $dbCache = array(); if ($this->dbCacheTable) { $db =& $this->registry->modules[ISSO_DB_LAYER]; $cache = $db->query("SELECT * FROM {$this->dbCacheTable} WHERE filename IN ('" . implode("', '", $namearray) . "')"); while ($tpl = $db->fetch_array($cache)) { $time = filemtime($this->registry->apppath . $this->templatedir . $tpl['filename'] . '.' . $this->extension); $template = $tpl['template']; if ($time > $tpl['timestamp']) { $template = $this->_parse($this->_load($tpl['filename'])); $db->query("UPDATE {$this->dbCacheTable} SET template = '" . $db->escape_string($template) . "', timestamp = " . TIMENOW . " WHERE filename = '" . $tpl['filename'] . "'"); $tpl['template'] = $template; } $dbCache["$tpl[filename]"] = $template; } } foreach ($namearray AS $name) { if (isset($dbCache["$name"])) { $template = $dbCache["$name"]; } else { $template = $this->_parse($this->_load($name)); if ($this->dbCacheTable) { $db->query("INSERT INTO {$this->dbCacheTable} (filename, template, timestamp) VALUES ('$name', '" . $db->escape_string($template) . "', " . TIMENOW . ")"); } } $this->cache["$name"] = $template; $this->usage["$name"] = 0; } } } // ################################################################### /** * Loads a template from the file system from the specified * $templatedir with the file extension $extension * * @access private * * @param string The name of the template call */ function _load($name) { $path = $this->registry->apppath . $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'", E_USER_ERROR); exit; } } else { trigger_error("Could not load the template '$path'", E_USER_ERROR); exit; } } } /*=====================================================================*\ || ################################################################### || # $HeadURL$ || # $Id$ || ################################################################### \*=====================================================================*/ ?>