Source for file Engine.class.php
Documentation is available at Engine.class.php
* Gumbo Library Framework
* This library is being released under the terms of the New BSD License. A
* copy of the license is packaged with the software (LICENSE.txt). If no
* copy is found, a copy of the license template can be found at:
* http://www.opensource.org/licenses/bsd-license.php
* @copyright Copyright (c) 2007, iBayou, Michael Luster
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @author Michael Luster <mluster79@yahoo.com>
* @link http://sourceforge.net/projects/phpgumbo
* Abstract Template Engine Class
* A Template Engine defines common methods involved in running template systems.
* There are many different ways to use template (PHP includes, string replacement,
* Smarty). A Template system will basically load a file, replacing keywords
* with values defined inside the program. Each Engine will define a specific
* implementation for parsing Template files.
* The class provides a basic implementation. This allows an Engine to only
* override certain methods for custom implementation. The common methods allow
* access to Template variables (and assignment), setting the Template file name
* (file name or full path), setting the Template directory path (including extra
* Smarty defined paths), loading global Template settings, and setting the
* surrounding characters of keywords inside the Template file.
* To create a new Template_Engine, simply extend this class, placing the new
* file into the 'gumbo/template/engine/*' directory. The name should be
* unique 'Gumbo_Template_Engine_[Name]'. The new Engine will be responsible
* for parsing a particular type of Template file. Details on how to use the
* Engine should be defined inside the class.
* The Template File can either be a simple file name (combined with the Template
* Directory) or a full path. When the Template File is parsed, the 'getFullPath'
* $this->setFile ("my_file.txt");
* $this->setDirTpl ("/path/to/file/");
* $this->getFullPath (); // outputs '/path/to/file/my_file.txt', if file exists
* Template Variables are assigned keywords referencing a value determined by the
* program code. The assigned keys are paired with the Template file, replacing
* the 'keyword' with the value. Template Variables can be any type, however check
* the specific Engine on any restrictions defined.
* $this->assign ("key", my_value);
* This is simply a left and right character string surrounding keywords, helping
* to separate raw text. Inside a basic string replacement Engine, curly braces
* could surround the keyword '{keyword}' (or any other characters). The parser
* (if used) should find these replacements.
* @copyright Copyright (c) 2007, iBayou, Michael Luster
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @author Michael Luster <mluster79@yahoo.com>
* @link http://sourceforge.net/projects/phpgumbo
* @desc Abstract Template Engine Class
gumbo_load ("Interface_Output");
/** @var array $_vars assigned tempalte variables as key=>val pair */
/** @var string $_file template file name */
/** @var string $_dir_tpl template file directory */
/** @var string $_dir_compile template file compile directory */
/** @var string $_dir_cache template file cache directory */
/** @var string $_dir_config template file config directory */
/** @var string $_left_del left delimeter */
/** @var string $_right_del right delimeter */
/** @var bool $_formatted if the template has been formatted into a string */
/** @var string $_output formatted template string */
* Parses the template file into an HTML string
* @precondition setFormatted (false)
* @precondition setOutput (null)
* @postcondition isFormatted ()
abstract protected function parse ();
* Assigns a template variable with a value
* @param string $key template file reference
* @param mixed $val primitive type or Gumbo_Interface_Output
public function __set ($key, $val) {
* Returns the value of a template key reference
public function __get ($key) {
* Returns if the template reference key isset
return isset
($this->_vars [$key]);
* Unassigns a template reference key
unset
($this->_vars [$key]);
* Displays the formatted template file
* Returns the formatted template file string
* @param bool $reload parses the template file again
* @throws Gumbo_Exception
public function fetch ($reload=
false) {
$e->setFunction (__METHOD__
);
// check if the template has been formatted
return $this->getHtml ();
$e->setFunction (__METHOD__
);
* Assigns a template variable with a value
* @param string|array$key template file reference
* @throws Gumbo_Exception
public function assign ($key, $val=
null) {
foreach ($key as $ref=>
$value) {
$this->_vars [$key] =
$val;
$e->setFunction (__METHOD__
);
* Unassigns a template variable
* @throws Gumbo_Exception
if (!isset
($this->_vars [$key])) {
unset
($this->_vars [$key]);
$e->setFunction (__METHOD__
);
* Resets the template variables (useful for reloading a List)
* @postcondition !getVars()
* @postcondition !isFormatted()
public function reset () {
* Loads the global template variables (does not load global Template variables)
* @param bool $assign_global_vars also assigns global variables
* @throws Gumbo_Exception
public function loadGlobals ($assign_global_vars=
false) {
if (!is_bool ($assign_global_vars)) {
throw
new Gumbo_Exception ("Invalid Argument 'assign_global_vars:bool' => {$assign_global_vars}:" .
gettype ($assign_global_vars));
$e->setFunction (__METHOD__
);
$assign_global_vars =
false;
if ($assign_global_vars) {
* Sets the template file name
$e->setFunction (__METHOD__
);
* Sets the template file directory
* @precondition is_dir ($loc)
* @throws Gumbo_Exception
$e->setFunction (__METHOD__
);
* Sets the template file compile directory
* @precondition is_dir ($loc)
* @throws Gumbo_Exception
$e->setFunction (__METHOD__
);
* Sets the template file cache directory
* @precondition is_dir ($loc)
* @throws Gumbo_Exception
$e->setFunction (__METHOD__
);
* Sets the template file config directory
* @precondition is_dir ($loc)
* @throws Gumbo_Exception
$e->setFunction (__METHOD__
);
* Sets the left delimeter value
* @throws Gumbo_Exception
$e->setFunction (__METHOD__
);
* Sets the right delimeter value
* @throws Gumbo_Exception
$e->setFunction (__METHOD__
);
* Sets the template file as formatted
* @throws Gumbo_Exception
$e->setFunction (__METHOD__
);
* Sets the formatted output template string
* @throws Gumbo_Exception
$e->setFunction (__METHOD__
);
* Returns the full path to the Template file
* Returns the template file name
* Returns the template file directory
* Returns the template file compile directory
* Returns the template file cache directory
* Returns the template file config directory
* Returns the value of the key reference
* @param string $key reference key
* @throws Gumbo_Exception
public function getVar ($key) {
if (!isset
($this->_vars [$key])) {
return $this->_vars [$key];
$e->setFunction (__METHOD__
);
* Returns all the template variables
* Returns the left delimeter
* Returns the right delimeter
* Returns if the template has been formatted
* Returns the formatted output template string
* Returns if the template file exists
if (!$this->getFile ()) { return false; }