Source for file Template.class.php
Documentation is available at Template.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
* The Template Class is responsible for saving global Template values and loading
* the appropriate Template Engine. The program can easily use the global settings
* to easily pull the appropriate Engine without defining the specific engine. For
* example, if the application uses one type of Template Engine throughout, this
* class will automatically load the defined Engine. All properties are static
* to preserve their state.
* $tpl = new Gumbo_Template ();
* $tpl->factory ()->engineMethod ();
* @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
gumbo_load ("Interface_Template");
gumbo_load ("Interface_Factory");
class Gumbo_Template implements Gumbo_Interface_Template, Gumbo_Interface_Factory {
/** @var string $_global_engine default Template engine for template files */
private static $_global_engine;
/** @var array $_global_vars global Template variables */
private static $_global_vars =
array ();
/** @var string $_global_dir_tpl global Template directory location */
private static $_global_dir_tpl;
/** @var string $_global_dir_compile global Template compile directory */
private static $_global_dir_compile;
/** @var string $_global_dir_cache global Template cache directory */
private static $_global_dir_cache;
/** @var string $_global_dir_config global Template config directory */
private static $_global_dir_config;
/** @var string $_global_left_del global left delimeter */
private static $_global_left_del;
/** @var string $_global_right_del global right delimeter */
private static $_global_right_del;
* Creates a new Template_Engine object
* @param string $name template file name (optional)
* @param string $args name of the engine
* @return Gumbo_Interface_Template_Engine
* @throws Gumbo_Exception
public function factory ($name=
null, $args=
null) {
throw
new Gumbo_Exception ("Template Engine Not Found: Gumbo_Template_Engine_{$name}");
$engine =
"Gumbo_Template_Engine_{$name}";
$e->setFunction (__METHOD__
);
* Assigns a global template variable
* All global template variable references will be prepended with
* "global_*" string. This will separate the global variables from
* local template variables.
* @precondition must be a primitive type
* @param string|array$key template reference
* @param mixed $val template value
* @throws Gumbo_Exception
public static function assign ($key, $val=
null) {
// loop through if the key is an array
foreach ($key as $ref=>
$value) {
// verify primitive value type
throw
new Gumbo_Exception ("Invalid Argument 'val:bool|str|num|null|Gumbo_Interface_Output' => {$val}:" .
gettype ($val));
} catch
(Gumbo_Exception $e) {
$e->setFunction (__METHOD__
);
* Removes the global variable assignment
* @param string $key template variable reference
* @throws Gumbo_Exception
public static function unassign ($key) {
throw
new Gumbo_Exception ("Invalid Key: {$key}");
} catch
(Gumbo_Exception $e) {
$e->setFunction (__METHOD__
);
* Sets the global template engine to use
* @throws Gumbo_Exception
throw
new Gumbo_Exception ("Template Engine Not Found: Gumbo_Template_Engine_{$engine}");
} catch
(Gumbo_Exception $e) {
$e->setFunction (__METHOD__
);
* Sets the global template file directory
* @precondition is_dir ($loc)
* @throws Gumbo_Exception
} catch
(Gumbo_Exception $e) {
$e->setFunction (__METHOD__
);
* Sets the global template file compile directory
* @precondition is_dir ($loc)
* @throws Gumbo_Exception
} catch
(Gumbo_Exception $e) {
$e->setFunction (__METHOD__
);
* Sets the global template file cache directory
* @precondition is_dir ($loc)
* @throws Gumbo_Exception
} catch
(Gumbo_Exception $e) {
$e->setFunction (__METHOD__
);
* Sets the global template file config directory
* @precondition is_dir ($loc)
* @throws Gumbo_Exception
} catch
(Gumbo_Exception $e) {
$e->setFunction (__METHOD__
);
* Sets the global left delimeter value
* @throws Gumbo_Exception
} catch
(Gumbo_Exception $e) {
$e->setFunction (__METHOD__
);
* Sets the global right delimeter value
* @throws Gumbo_Exception
} catch
(Gumbo_Exception $e) {
$e->setFunction (__METHOD__
);
* Returns the global template engine
* Returns a global template variable value
* @param string $key variable reference
* @throws Gumbo_Exception
public static function getVar ($key) {
throw
new Gumbo_Exception ("Invalid Key: {$key}");
} catch
(Gumbo_Exception $e) {
$e->setFunction (__METHOD__
);
* Returns all the global template variables
public static function getVars () {
* Returns the global Template directory
return Gumbo_Template::$_global_dir_tpl;
* Returns the global Template compile directory
return Gumbo_Template::$_global_dir_compile;
* Returns the global Template cache directory
return Gumbo_Template::$_global_dir_cache;
* Returns the global Template config directory
return Gumbo_Template::$_global_dir_config;
* Returns the global left delimeter
return Gumbo_Template::$_global_left_del;
* Returns the global right delimeter
return Gumbo_Template::$_global_right_del;
* Returns if the Template Engine exists
* @throws Gumbo_Exception
public static function isEngine ($engine) {
if (!is_string ($engine)) {
$name =
"Gumbo_Template_Engine_" .
ucfirst ($engine);
return gumbo_load ($name);
$e->setFunction (__METHOD__
);