Source for file Template.class.php

Documentation is available at Template.class.php

  1. <?php
  2. /**
  3.  * Gumbo Library Framework
  4.  *
  5.  * LICENSE
  6.  * This library is being released under the terms of the New BSD License.  A
  7.  * copy of the license is packaged with the software (LICENSE.txt).  If no
  8.  * copy is found, a copy of the license template can be found at:
  9.  * http://www.opensource.org/licenses/bsd-license.php
  10.  * 
  11.  * @category Gumbo
  12.  * @package Template
  13.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  14.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  15.  * @author Michael Luster <mluster79@yahoo.com>
  16.  * @link http://sourceforge.net/projects/phpgumbo
  17.  * @version 0.0.1
  18.  */
  19.  
  20. /**
  21.  * Template Class
  22.  * 
  23.  * The Template Class is responsible for saving global Template values and loading
  24.  * the appropriate Template Engine.  The program can easily use the global settings
  25.  * to easily pull the appropriate Engine without defining the specific engine.  For
  26.  * example, if the application uses one type of Template Engine throughout, this
  27.  * class will automatically load the defined Engine.  All properties are static
  28.  * to preserve their state.
  29.  * 
  30.  * <pre>
  31.  * $tpl = new Gumbo_Template ();
  32.  * $tpl->factory ()->engineMethod ();
  33.  * </pre>
  34.  *
  35.  * @category Gumbo
  36.  * @package Template
  37.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  38.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  39.  * @author Michael Luster <mluster79@yahoo.com>
  40.  * @link http://sourceforge.net/projects/phpgumbo
  41.  * @desc Template Class
  42.  * @version 0.0.1
  43.  */
  44.  
  45. gumbo_load ("Interface_Template");
  46. gumbo_load ("Interface_Factory");
  47.  
  48. class Gumbo_Template implements Gumbo_Interface_TemplateGumbo_Interface_Factory {
  49.     
  50.     /** @var string $_global_engine default Template engine for template files */
  51.     private static $_global_engine;
  52.     /** @var array $_global_vars global Template variables */
  53.     private static $_global_vars array ();
  54.     
  55.     /** @var string $_global_dir_tpl global Template directory location */
  56.     private static $_global_dir_tpl;
  57.     /** @var string $_global_dir_compile global Template compile directory */
  58.     private static $_global_dir_compile;
  59.     /** @var string $_global_dir_cache global Template cache directory */
  60.     private static $_global_dir_cache;
  61.     /** @var string $_global_dir_config global Template config directory */
  62.     private static $_global_dir_config;
  63.     
  64.     /** @var string $_global_left_del global left delimeter */
  65.     private static $_global_left_del;
  66.     /** @var string $_global_right_del global right delimeter */
  67.     private static $_global_right_del;
  68.     
  69.     
  70.     
  71.     /** ACTION METHODS **/
  72.     /**
  73.      * Creates a new Template_Engine object
  74.      * @param string $name template file name (optional)
  75.      * @param string $args name of the engine
  76.      * @return Gumbo_Interface_Template_Engine 
  77.      * @throws Gumbo_Exception
  78.      * @uses Gumbo_Load
  79.      */
  80.     public function factory ($name=null$args=null{
  81.         // verify precondition
  82.         if (is_null ($name|| !is_string ($name)) {
  83.             $name Gumbo_Template::getEngine ();
  84.         }
  85.         
  86.         try {
  87.             ucfirst ($name);
  88.             if (!Gumbo_Template::isEngine ($name)) {
  89.                 throw new Gumbo_Exception ("Template Engine Not FoundGumbo_Template_Engine_{$name}");
  90.             }
  91.             
  92.             $engine "Gumbo_Template_Engine_{$name}";
  93.             return new $engine (func_get_arg (1));
  94.         catch (Gumbo_Exception $e{
  95.             $e->setFunction (__METHOD__);
  96.             gumbo_trigger ($e);
  97.         }
  98.         return null;
  99.     }
  100.     
  101.     
  102.     
  103.     /** STATIC METHODS **/
  104.     /**
  105.      * Assigns a global template variable
  106.      * 
  107.      * All global template variable references will be prepended with
  108.      * "global_*" string.  This will separate the global variables from
  109.      * local template variables.
  110.      * 
  111.      * @precondition must be a primitive type
  112.      * @param string|array$key template reference
  113.      * @param mixed $val template value
  114.      * @throws Gumbo_Exception
  115.      */
  116.     public static function assign ($key$val=null{
  117.         try {
  118.             // verify precondition
  119.             if (!is_string ($key&& !is_array ($key)) {
  120.                 throw new Gumbo_Exception ("Invalid Argument 'key:str|arr' => {$key}:gettype ($key));
  121.             }
  122.             
  123.             // loop through if the key is an array
  124.             if (is_array ($key)) {
  125.                 foreach ($key as $ref=>$value{
  126.                     Gumbo_Template::assign ($ref$value);
  127.                 }
  128.                 return;
  129.             }
  130.             
  131.             // verify primitive value type
  132.             if (!is_null ($val&& !is_bool ($val&& !is_string ($val&& !is_numeric ($val&& !is_object ($val)) {
  133.                 throw new Gumbo_Exception ("Invalid Argument 'val:bool|str|num|null|Gumbo_Interface_Output' => {$val}:gettype ($val));
  134.             }
  135.             if (is_object ($val&& !($val instanceof Gumbo_Interface_Output)) {
  136.                 throw new Gumbo_Exception ("Invalid Argument 'val:Gumbo_Interface_Output' => {$val}:gettype ($val));
  137.             }
  138.             $key "global_" $key;
  139.             Gumbo_Template::$_global_vars [$key$val;
  140.         catch (Gumbo_Exception $e{
  141.             $e->setFunction (__METHOD__);
  142.             gumbo_trigger ($e);
  143.         }
  144.     }
  145.     
  146.     /**
  147.      * Removes the global variable assignment
  148.      * @param string $key template variable reference
  149.      * @throws Gumbo_Exception
  150.      */
  151.     public static function unassign ($key{
  152.         try {
  153.             // verify precondition
  154.             if (!is_string ($key)) {
  155.                 throw new Gumbo_Exception ("Invalid Argument 'key:str' => {$key}:gettype ($key));
  156.             }
  157.             if (!isset (Gumbo_Template::$_global_vars [$key])) {
  158.                 throw new Gumbo_Exception ("Invalid Key: {$key}");
  159.             }
  160.             
  161.             unset (Gumbo_Template::$_global_vars [$key]);
  162.         catch (Gumbo_Exception $e{
  163.             $e->setFunction (__METHOD__);
  164.             gumbo_trigger ($e);
  165.         }
  166.     }
  167.     
  168.     
  169.     
  170.     /** MUTATOR METHODS **/
  171.     /**
  172.      * Sets the global template engine to use
  173.      * @param string $engine 
  174.      * @throws Gumbo_Exception
  175.      */
  176.     public static function setEngine ($engine{
  177.         try {
  178.             // verify precondition
  179.             if (!is_string ($engine)) {
  180.                 throw new Gumbo_Exception ("Invalid Argument 'engine:str' => {$engine}:gettype ($engine));
  181.             }
  182.             ucfirst ($engine);
  183.             if (!Gumbo_Template::isEngine ($engine)) {
  184.                 throw new Gumbo_Exception ("Template Engine Not FoundGumbo_Template_Engine_{$engine}");
  185.             }
  186.             
  187.             Gumbo_Template::$_global_engine $engine;
  188.         catch (Gumbo_Exception $e{
  189.             $e->setFunction (__METHOD__);
  190.             gumbo_trigger ($e);
  191.         }
  192.     }
  193.     
  194.     /**
  195.      * Sets the global template file directory
  196.      * @precondition is_dir ($loc)
  197.      * @param string $loc 
  198.      * @throws Gumbo_Exception
  199.      */
  200.     public static function setDirTpl ($loc{
  201.         try {
  202.             // verify precondition
  203.             if (!is_string ($loc)) {
  204.                 throw new Gumbo_Exception ("Invalid Argument 'loc:str' => {$loc}:gettype ($loc));
  205.             }
  206.             if (!is_dir ($loc)) {
  207.                 throw new Gumbo_Exception ("Directory Not Found: {$loc}");
  208.             }
  209.             
  210.             Gumbo_Template::$_global_dir_tpl $loc;
  211.         catch (Gumbo_Exception $e{
  212.             $e->setFunction (__METHOD__);
  213.             gumbo_trigger ($e);
  214.         }
  215.     }
  216.     
  217.     /**
  218.      * Sets the global template file compile directory
  219.      * @precondition is_dir ($loc)
  220.      * @param string $loc 
  221.      * @throws Gumbo_Exception
  222.      */
  223.     public static function setDirCompile ($loc{
  224.         try {
  225.             // verify precondition
  226.             if (!is_string ($loc)) {
  227.                 throw new Gumbo_Exception ("Invalid Argument 'loc:str' => {$loc}:gettype ($loc));
  228.             }
  229.             if (!is_dir ($loc)) {
  230.                 throw new Gumbo_Exception ("Directory Not Found: {$loc}");
  231.             }
  232.             
  233.             Gumbo_Template::$_global_dir_compile $loc;
  234.         catch (Gumbo_Exception $e{
  235.             $e->setFunction (__METHOD__);
  236.             gumbo_trigger ($e);
  237.         }
  238.     }
  239.     
  240.     /**
  241.      * Sets the global template file cache directory
  242.      * @precondition is_dir ($loc)
  243.      * @param string $loc 
  244.      * @throws Gumbo_Exception
  245.      */
  246.     public static function setDirCache ($loc{
  247.         try {
  248.             // verify precondition
  249.             if (!is_string ($loc)) {
  250.                 throw new Gumbo_Exception ("Invalid Argument 'loc:str' => {$loc}:gettype ($loc));
  251.             }
  252.             if (!is_dir ($loc)) {
  253.                 throw new Gumbo_Exception ("Directory Not Found: {$loc}");
  254.             }
  255.             
  256.             Gumbo_Template::$_global_dir_cache $loc;
  257.         catch (Gumbo_Exception $e{
  258.             $e->setFunction (__METHOD__);
  259.             gumbo_trigger ($e);
  260.         }
  261.     }
  262.     
  263.     /**
  264.      * Sets the global template file config directory
  265.      * @precondition is_dir ($loc)
  266.      * @param string $loc 
  267.      * @throws Gumbo_Exception
  268.      */
  269.     public static function setDirConfig ($loc{
  270.         try {
  271.             // verify precondition
  272.             if (!is_string ($loc)) {
  273.                 throw new Gumbo_Exception ("Invalid Argument 'loc:str' => {$loc}:gettype ($loc));
  274.             }
  275.             if (!is_dir ($loc)) {
  276.                 throw new Gumbo_Exception ("Directory Not Found: {$loc}");
  277.             }
  278.             
  279.             Gumbo_Template::$_global_dir_config $loc;
  280.         catch (Gumbo_Exception $e{
  281.             $e->setFunction (__METHOD__);
  282.             gumbo_trigger ($e);
  283.         }
  284.     }
  285.     
  286.     /**
  287.      * Sets the global left delimeter value
  288.      * @param string $val 
  289.      * @throws Gumbo_Exception
  290.      */
  291.     public static function setLeftDelimeter ($val{
  292.         try {
  293.             // verify precondition
  294.             if (!is_string ($val)) {
  295.                 throw new Gumbo_Exception ("Invalid Argument 'val:str' => {$val}:gettype ($val));
  296.             }
  297.             
  298.             Gumbo_Template::$_global_left_del $val;
  299.         catch (Gumbo_Exception $e{
  300.             $e->setFunction (__METHOD__);
  301.             gumbo_trigger ($e);
  302.         }
  303.     }
  304.     
  305.     /**
  306.      * Sets the global right delimeter value
  307.      * @param string $val 
  308.      * @throws Gumbo_Exception
  309.      */
  310.     public static function setRightDelimeter ($val{
  311.         try {
  312.             // verify precondition
  313.             if (!is_string ($val)) {
  314.                 throw new Gumbo_Exception ("Invalid Argument 'val:str' => {$val}:gettype ($val));
  315.             }
  316.             
  317.             Gumbo_Template::$_global_right_del $val;
  318.         catch (Gumbo_Exception $e{
  319.             $e->setFunction (__METHOD__);
  320.             gumbo_trigger ($e);
  321.         }
  322.     }
  323.     
  324.     
  325.     
  326.     /** ACCESSOR METHODS **/
  327.     /**
  328.      * Returns the global template engine
  329.      * @return string 
  330.      */
  331.     public static function getEngine ({
  332.         return Gumbo_Template::$_global_engine;
  333.     }
  334.     
  335.     /**
  336.      * Returns a global template variable value
  337.      * @param string $key variable reference
  338.      * @return mixed 
  339.      * @throws Gumbo_Exception
  340.      */
  341.     public static function getVar ($key{
  342.         try {
  343.             // verify precondition
  344.             if (!is_string ($key)) {
  345.                 throw new Gumbo_Exception ("Invalid Argument 'key:str' => {$key}:gettype ($key));
  346.             }
  347.             if (!isset (Gumbo_Template::$_global_vars [$key])) {
  348.                 throw new Gumbo_Exception ("Invalid Key: {$key}");
  349.             }
  350.             
  351.             return Gumbo_Template::$_global_vars [$key];
  352.         catch (Gumbo_Exception $e{
  353.             $e->setFunction (__METHOD__);
  354.             gumbo_trigger ($e);
  355.         }
  356.         return null;
  357.     }
  358.     
  359.     /**
  360.      * Returns all the global template variables
  361.      * @return array 
  362.      */
  363.     public static function getVars ({
  364.         return Gumbo_Template::$_global_vars;
  365.     }
  366.     
  367.     /**
  368.      * Returns the global Template directory
  369.      * @return string 
  370.      */
  371.     public static function getDirTpl ({
  372.         return Gumbo_Template::$_global_dir_tpl;
  373.     }
  374.     
  375.     /**
  376.      * Returns the global Template compile directory
  377.      * @return string 
  378.      */
  379.     public static function getDirCompile ({
  380.         return Gumbo_Template::$_global_dir_compile;
  381.     }
  382.     
  383.     /**
  384.      * Returns the global Template cache directory
  385.      * @return string 
  386.      */
  387.     public static function getDirCache ({
  388.         return Gumbo_Template::$_global_dir_cache;
  389.     }
  390.     
  391.     /**
  392.      * Returns the global Template config directory
  393.      * @return string 
  394.      */
  395.     public static function getDirConfig ({
  396.         return Gumbo_Template::$_global_dir_config;
  397.     }
  398.     
  399.     /**
  400.      * Returns the global left delimeter
  401.      * @return string 
  402.      */
  403.     public static function getLeftDelimeter ({
  404.         return Gumbo_Template::$_global_left_del;
  405.     }
  406.     
  407.     /**
  408.      * Returns the global right delimeter
  409.      * @return string 
  410.      */
  411.     public static function getRightDelimeter ({
  412.         return Gumbo_Template::$_global_right_del;
  413.     }
  414.     
  415.     
  416.     
  417.     /**
  418.      * Returns if the Template Engine exists
  419.      * @param string $engine 
  420.      * @return bool 
  421.      * @throws Gumbo_Exception
  422.      * @uses Gumbo_Load
  423.      */
  424.     public static function isEngine ($engine{
  425.         try {
  426.             // verify precondition
  427.             if (!is_string ($engine)) {
  428.                 throw new Gumbo_Exception ("Invalid Argument 'engine:str' => {$engine}:gettype ($engine));
  429.             }
  430.             
  431.             $name "Gumbo_Template_Engine_" ucfirst ($engine);
  432.             return gumbo_load ($name);
  433.         catch (Gumbo_Exception $e{
  434.             $e->setFunction (__METHOD__);
  435.             gumbo_trigger ($e);
  436.         }
  437.         return false;
  438.     }
  439.     
  440. }
  441.  
  442. ?>