Source for file Basic.class.php

Documentation is available at Basic.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.  * Basic Template Engine Class
  22.  * 
  23.  * This Engine will perform a simple string replacement with the
  24.  * template variables.  The template file will hold references
  25.  * to the template.  The references will be contained within
  26.  * left and right delimeters.  A template reference will look
  27.  * like:
  28.  * {reference}
  29.  * 
  30.  * The left and right delimeters can be altered from the defaults.  It's
  31.  * important that these values are accurate, for the replacement of the
  32.  * references will not work.
  33.  *
  34.  * @category Gumbo
  35.  * @package Template
  36.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  37.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  38.  * @author Michael Luster <mluster79@yahoo.com>
  39.  * @link http://sourceforge.net/projects/phpgumbo
  40.  * @desc Basic Template Engine Class
  41.  * @version 0.0.1
  42.  */
  43.  
  44. gumbo_load ("Template_Engine");
  45.  
  46.     
  47.     /**
  48.      * Constructor
  49.      * @param string $file template file name
  50.      */
  51.     public function __construct ($file=null{
  52.         if (!is_null ($file)) $this->setFile ($file)}
  53.         
  54.         $this->setLeftDelimeter ("{");
  55.         $this->setRightDelimeter ("}");
  56.     }
  57.     
  58.     
  59.     
  60.     /** ACTION METHODS **/
  61.     /**
  62.      * Parses the template file into an HTML string
  63.      * @precondition $this->setFormatted (false)
  64.      * @precondition $this->setHtml (null)
  65.      * @postcondition $this->isFormatted ()
  66.      */
  67.     protected function parse ({
  68.         // verify precondition
  69.         $this->setFormatted (false);
  70.         $this->setOutput (null);
  71.         
  72.         $contents file_get_contents ($this->getFullPath ());
  73.         
  74.         // loop through the template variables
  75.         foreach ($this->getVars (as $key=>$val{
  76.             $ref $this->getLeftDelimeter ($key $this->getRightDelimeter ();
  77.             if (is_object ($val&& $val instanceof Gumbo_Interface_Output{
  78.                 $val $val->fetch ();
  79.             }
  80.             $contents str_replace ($ref$val$contents);
  81.         }
  82.         
  83.         $this->setOutput ($contents);
  84.         $this->setFormatted (true);
  85.     }
  86.     
  87.     
  88.     
  89.     /** MUTATOR METHODS **/
  90.     /**
  91.      * Assigns a template variable with a value
  92.      * @precondition must be primitive type
  93.      * @param string|array$key template file reference
  94.      * @param mixed $val (primitive type or Gumbo_Interface_Output)
  95.      * @throws Gumbo_Exception
  96.      */
  97.     public function assign ($key$val=null{
  98.         try {
  99.             // verify precondition
  100.             if (!is_string ($key&& !is_array ($key)) {
  101.                 throw new Gumbo_Exception ("Invalid Argument 'key:str|arr' => {$key}:gettype ($key));
  102.             }
  103.             
  104.             // check for an array
  105.             if (is_array ($key)) {
  106.                 foreach ($key as $ref=>$value{
  107.                     $this->assign ($ref$value);
  108.                 }
  109.                 return;
  110.             }
  111.             
  112.             // verify precondition
  113.             // ensures only a primitive type is assigned to this engine
  114.             if (!is_null ($val&& !is_bool ($val&& !is_string ($val&& !is_numeric ($val&& !is_object ($val)) {
  115.                  throw new Gumbo_Exception ("Invalid Argument 'val:bool|num|str|null|Gumbo_Interface_Output' => {$key}:gettype ($val))
  116.             }
  117.             
  118.             // verify the Object is a Gumbo_Template_Engine
  119.             if (is_object ($val&& !($val instanceof Gumbo_Interface_Output)) {
  120.                 throw new Gumbo_Exception ("Invalid Argument 'val:Gumbo_Interface_Output' => {$val}:gettype ($val));
  121.             }
  122.             
  123.             // default assignment
  124.             $this->_vars [$key$val;
  125.         catch (Gumbo_Exception $e{
  126.             $e->setFunction (__METHOD__);
  127.             gumbo_trigger ($e);
  128.         }
  129.     }
  130.     
  131. }
  132.  
  133. ?>