DotLibrary
[ class tree: DotLibrary ] [ index: DotLibrary ] [ all elements ]

Source for file Template.php

Documentation is available at Template.php

  1. <?php
  2. /**
  3. * DotBoost Technologies Inc.
  4. * DotKernel Application Framework
  5. *
  6. @category   DotKernel
  7. @package    DotLibrary
  8. @copyright  Copyright (c) 2009 DotBoost  Technologies (http://www.dotboost.com)
  9. @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
  10. @version    $Id: Template.php 158 2010-06-25 08:59:20Z teo $
  11. */
  12.  
  13. /**
  14. * Template engine, based on PHPLIB library
  15. @category   DotKernel
  16. @package    DotLibrary
  17. @author     DotKernel Team <team@dotkernel.com>
  18. */
  19.  
  20.  
  21. {
  22.     /**
  23.      * If set, echo assignments
  24.      * @access private
  25.      * @var bool 
  26.      */
  27.     private $debug = false;
  28.     /**
  29.      * If set, echo blocks time parse
  30.      * @access private
  31.      * @var bool 
  32.      */
  33.     private $debugBlock = false;
  34.     /**
  35.      * Relative filenames are relative to this pathname
  36.      * @access private
  37.      * @var string 
  38.      */
  39.     private $root = '..';
  40.     /**
  41.      * $file[handle] = 'filename';
  42.      * @access private
  43.      * @var array 
  44.      */
  45.     private $file = array();
  46.     /**
  47.      * fallback paths that should be defined in a child class
  48.      * @access private
  49.      * @var array 
  50.      */
  51.     private $fileFallbacks = array();
  52.     /**
  53.      * $varkeys[key] = 'key'
  54.      * @access private
  55.      * @var array 
  56.      */
  57.     private $varkeys = array();
  58.     /**
  59.      * $varvals[key] = 'value';
  60.      * @access private
  61.      * @var array 
  62.      */
  63.     private $varvals = array();
  64.     /**
  65.      * 'remove'  => remove undefined variables
  66.      * 'comment' => replace undefined variables with comments
  67.      * 'keep'    => keep undefined variables
  68.      * @access private
  69.      * @var string 
  70.      */
  71.     private $unknowns = 'remove';
  72.     /**
  73.      * 'yes' => halt,
  74.      * 'report' => report error, continue,
  75.      * 'no' => ignore error quietly
  76.      * @access private
  77.      * @var string 
  78.      */
  79.     private $haltOnError = 'yes';
  80.     /**
  81.      * The last error message is retained here
  82.      * @access private
  83.      * @var string 
  84.      * @see halt
  85.      */
  86.     private $lastError = '';
  87.     /**
  88.      * Determines whether Template outputs filename comments.
  89.      * false = no filename outputs
  90.      * true = HTML comments (e.g. <!-- START FILE $filename -->) placed in output
  91.      * @access private
  92.      * @var string 
  93.      */
  94.     private $filenameComments = false;
  95.     /**
  96.      * Determines the regular expression used to find unknown variable tags.
  97.      * 'loose'  = traditional match all curly braces with no whitespace between
  98.      * 'strict' = adopts PHP's variable naming rules
  99.      * @access private
  100.      * @var bool 
  101.      */
  102.     private $unknownRegexp = 'loose';
  103.     /**
  104.      * Start time
  105.      * @access private
  106.      * @var array 
  107.      */
  108.     private $start_time = array();
  109.     /**
  110.      * End time
  111.      * @access private
  112.      * @var array 
  113.      */
  114.     private $endTime = array();
  115.     /**
  116.      * Singleton instance
  117.      * @access protected
  118.      * @static
  119.      * @var Dot_Template 
  120.      */
  121.     protected static $_instance null;    
  122.     /**
  123.      * Singleton pattern implementation makes 'new' unavailable
  124.      * @access public
  125.      * @param string $root     Template root directory
  126.      * @param string $unknowns How to handle unknown variables
  127.      * @param array  $fallback Fallback paths
  128.      * @return void 
  129.      */
  130.     protected function __construct($root '.'$unknowns 'remove'$fallback='')
  131.     {
  132.         $this->setRoot($root);
  133.         $this->setUnknowns($unknowns);
  134.         if (is_array($fallback)) $this->fileFallbacks = $fallback;
  135.     }
  136.     /**
  137.      * Singleton pattern implementation makes 'clone' unavailable
  138.      * @access protected
  139.      * @return void 
  140.      */
  141.     protected function __clone()
  142.     {}
  143.     /**
  144.      * Returns an instance of Dot_View
  145.      * Singleton pattern implementation
  146.      * @access public
  147.      * @param string $root     Template root directory
  148.      * @param string $unknowns How to handle unknown variables
  149.      * @param array  $fallback Fallback paths
  150.      * @return Dot_Template 
  151.      */
  152.     public static function getInstance($root '.'$unknowns 'remove'$fallback='')
  153.     {
  154.         if (null === self::$_instance
  155.         {
  156.             self::$_instance new self($root$unknowns$fallback);            
  157.         }
  158.         return self::$_instance;
  159.     }    
  160.     /**
  161.      * Checks that $root is a valid directory and if so sets this directory as the
  162.      * base directory from which templates are loaded by storing the value in
  163.      * $this->root. Relative filenames are prepended with the path in $this->root.
  164.      * @access public
  165.      * @return bool 
  166.      */
  167.     public function setRoot($root)
  168.     {
  169.         if(preg_match('-/$-'$root))
  170.         {
  171.             $root substr($root0-1);
  172.         }
  173.         if (!is_dir($root))
  174.         {
  175.             $this->halt("setRoot: $root is not a directory.");
  176.             return false;
  177.         }
  178.         $this->root = $root;
  179.         return true;
  180.     }
  181.     /**
  182.      * Start the time to measure something
  183.      * @access private
  184.      * @return array 
  185.      */
  186.     private function startTimer()
  187.     {
  188.         $mtime microtime ();
  189.         $mtime explode (' '$mtime);
  190.         $mtime $mtime[1$mtime[0];
  191.         return $mtime;
  192.     }
  193.     /**
  194.      * Return the end time
  195.      * @access private
  196.      * @return float 
  197.      */
  198.     private function endTimer($varname)
  199.     {
  200.         $mtime microtime ();
  201.         $mtime explode (' '$mtime);
  202.         $mtime $mtime[1$mtime[0];
  203.         $endtime $mtime;
  204.         $totaltime round(($endtime $this->start_time[$varname]),2);
  205.         return $totaltime;
  206.     }
  207.     /**
  208.      * Sets the policy for dealing with unresolved variable names.
  209.      * @access public
  210.      * @param string $unknowns - default = 'remove'
  211.      * @return void 
  212.      */
  213.     public function setUnknowns($unknowns 'remove')
  214.     {
  215.         $this->unknowns = $unknowns;
  216.     }
  217.     /**
  218.      * Inspired From PEAR HTML_Template_PHPLIB 1.4.0
  219.      * Checks if the given variable exists.
  220.      * When an array is given, it is checked if all variables exist.
  221.      * @access public
  222.      * @param string|array$var Variable to check
  223.      * @return bool 
  224.      */
  225.     public function exists($var)
  226.     {
  227.         if (is_array($var)) 
  228.         {
  229.                 $isset true;
  230.                 foreach ($var as $varname{
  231.                         $isset $isset isset($this->varVals[$varname]);
  232.                 }
  233.                 return $isset 0;
  234.         
  235.         else 
  236.         {
  237.                 return isset($this->varVals[$var]);
  238.         }
  239.     }
  240.     /**
  241.      * Defines a filename for the initial value of a variable.
  242.      * It may be passed either a varname and a file name as two strings or
  243.      * a hash of strings with the key being the varname and the value
  244.      * being the file name.
  245.      * The new mappings are stored in the array $this->file.
  246.      * The files are not loaded yet, but only when needed.
  247.      * USAGE: setFile(array $filelist = (string $varname => string $filename))
  248.      * or
  249.      * USAGE: setFile(string $varname, string $filename)
  250.      * @access public
  251.      * @param string $varname 
  252.      * @param string $filename 
  253.      * @return bool 
  254.      */
  255.     public function setFile($varname$filename '')
  256.     {
  257.         if (!is_array($varname))
  258.         {
  259.             if ($filename == '')
  260.             {
  261.                 $this->halt('setFile: For varname '.$varname .'filename is empty.');
  262.                 return false;
  263.             }
  264.             $this->file[$varname$this->filename($filename);
  265.             if ($this->file[$varname=== false{
  266.                     return false;
  267.             }
  268.         }
  269.         else
  270.         {
  271.             reset($varname);
  272.             while(list($v$feach($varname))
  273.             {
  274.                 if ($f == '')
  275.                 {
  276.                     $this->halt('setFile: For varname '.$v.' filename is empty.');
  277.                     return false;
  278.                 }
  279.                 $this->file[$v$this->filename($f);
  280.             }
  281.         }
  282.         return true;
  283.     }
  284.     /**
  285.      * A variable $parent may contain a variable block defined by:
  286.      * &lt;!-- BEGIN $varname --&gt; content &lt;!-- END $varname --&gt;.
  287.      * This public function removes that block from $parent and replaces it
  288.      * with a variable reference named $name.
  289.      * The block is inserted into the varkeys and varvals hashes. If $name is
  290.      * omitted, it is assumed to be the same as $varname.
  291.      * Blocks may be nested but care must be taken to extract the blocks in order
  292.      * from the innermost block to the outermost block.
  293.      * USAGE: setBlock(string $parent, string $varname, [string $name = ''])
  294.      * @access public
  295.      * @param string $parent 
  296.      * @param string $varname 
  297.      * @param string $name 
  298.      * @return bool 
  299.      */
  300.     public function setBlock($parent$varname$name '')
  301.     {
  302.         $this->start_time[$varname0;
  303.         $this->start_time[$varname$this->startTimer();
  304.         if (!$this->loadFile($parent))
  305.         {
  306.             $this->halt('setBlock: unable to load '.$parent);
  307.             return false;
  308.         }
  309.         if ($name == '')
  310.         {
  311.             $name $varname;
  312.         }
  313.  
  314.         $str $this->getVar($parent);
  315.         $reg '/[ \t]*<!--\s+BEGIN '.$varname.'\s+-->\s*?\n?(\s*.*?\n?)'
  316.              . '\s*<!--\s+END '.$varname.'\s+-->\s*?\n?/sm';
  317.         preg_match_all($reg$str$m);
  318.         if (!isset($m[1][0]))
  319.         {
  320.             $this->halt('setBlock: unable to set block '.$varname);
  321.             return false;
  322.         }
  323.         $str preg_replace($reg'{' $name '}'$str);
  324.         if (isset($m[1][0])) 
  325.         {
  326.             $this->setVar($varname$m[1][0]);
  327.         }
  328.         $this->setVar($parent$str);
  329.         return true;
  330.     }
  331.     /**
  332.      * Sets the value of a variable.
  333.      * It may be called with either a varname and a value as two strings or an
  334.      * an associative array with the key being the varname and the value being
  335.      * the new variable value.
  336.      * The public function inserts the new value of the variable into the $varkeys and
  337.      * $varvals hashes. It is not necessary for a variable to exist in these hashes
  338.      * before calling this public function.
  339.      * An optional third parameter allows the value for each varname to be appended
  340.      * to the existing variable instead of replacing it. The default is to replace.
  341.      * This feature was introduced after the 7.2d release.
  342.      * USAGE: setVar(string $varname, [string $value = ''], [boolean $append = false])
  343.      * or
  344.      * USAGE: setVar(array $varname = (string $varname => string $value),
  345.      * [mixed $dummy_var], [boolean $append = false])
  346.      * @access public
  347.      * @param mixed $varname 
  348.      * @param mixed $value 
  349.      * @param bool $append 
  350.      * @return bool 
  351.      */
  352.     public function setVar($varname$value ''$append false)
  353.     {
  354.         if (!is_array($varname))
  355.         {
  356.             if (!empty($varname))
  357.             {
  358.                 if ($this->debug 1)
  359.                 {
  360.                     printf("<b>setVar:</b> (with scalar) <b>%s</b> = '%s'<br>\n"$varnamehtmlentities($value));
  361.                 }
  362.                 $this->varkeys[$varname'/'.$this->varname($varname).'/';
  363.                 if ($append && isset($this->varvals[$varname]))
  364.                 {
  365.                     $this->varvals[$varname.= $value;
  366.                 }
  367.                 else     $this->varvals[$varname$value;
  368.             }
  369.         }
  370.         else
  371.         {
  372.             reset($varname);
  373.             while(list($k$veach($varname))
  374.             {
  375.                 if (!empty($k))
  376.                 {
  377.                     if ($this->debug 1)
  378.                     {
  379.                         printf("<b>setVar:</b> (with array) <b>%s</b> = '%s'<br>\n"$khtmlentities($v));
  380.                     }
  381.                     $this->varkeys[$k'/'.$this->varname($k).'/';
  382.                     if ($append && isset($this->varvals[$k]))
  383.                     {
  384.                         $this->varvals[$k.= $v;
  385.                     }
  386.                     else     
  387.                     {
  388.                         $this->varvals[$k$v;
  389.                     }
  390.                 }
  391.             }
  392.         }
  393.     }
  394.     /** 
  395.      * Unsets a variable completely.
  396.      * It may be called with either a varname as a string or an array with the
  397.      * values being the varnames to be cleared.
  398.      * The public function removes the variable from the $varkeys and $varvals hashes.
  399.      * It is not necessary for a variable to exist in these hashes before calling
  400.      * this public function.
  401.      * USAGE: unsetVar(string $varname)
  402.      * or
  403.      * USAGE: unsetVar(array $varname = (string $varname))
  404.      * @access public
  405.      * @param mixed $varname 
  406.      * @return void 
  407.      */
  408.     public function unsetVar($varname)
  409.     {
  410.         if (!is_array($varname))
  411.         {
  412.             if (!empty($varname))
  413.             {
  414.                 if ($this->debug 1)
  415.                 {
  416.                     printf("<b>unsetVar:</b> (with scalar) <b>%s</b><br>\n"$varname);
  417.                 }
  418.                 unset($this->varkeys[$varname]);
  419.                 unset($this->varvals[$varname]);
  420.             }
  421.         }
  422.         else
  423.         {
  424.             reset($varname);
  425.             while(list($k$veach($varname))
  426.             {
  427.                 if (!empty($v))
  428.                 {
  429.                     if ($this->debug 1)
  430.                     {
  431.                         printf("<b>unsetVar:</b> (with array) <b>%s</b><br>\n"$v);
  432.                     }
  433.                     unset($this->varkeys[$v]);
  434.                     unset($this->varvals[$v]);
  435.                 }
  436.             }
  437.         }
  438.     }
  439.     /**
  440.      * Fills in all the variables contained within the variable named
  441.      * $varname. The resulting value is returned as the public function result and the
  442.      * original value of the variable varname is not changed. The resulting string
  443.      * is not 'finished'.
  444.      * RETURNS: the value of the variable $varname with all variables substituted or FALSE if halted
  445.      * USAGE: subst(string $varname)
  446.      * @access public
  447.      * @param mixed $varname 
  448.      * @return mixed 
  449.      */
  450.     public function subst($varname)
  451.     {
  452.         $varvals_quoted array();
  453.         if (!$this->loadFile($varname))
  454.         {
  455.             $this->halt('subst: unable to load '.$varname);
  456.             return false;
  457.         }
  458.         // quote the replacement strings to prevent bogus stripping of special chars
  459.         reset($this->varvals);
  460.         while(list($k$veach($this->varvals))
  461.         {
  462.             $varvals_quoted[$kpreg_replace(array('/\\\\/''/\$/')array('\\\\\\\\''\\\\$')$v);
  463.         }
  464.         $str $this->getVar($varname);
  465.         $str preg_replace($this->varkeys$varvals_quoted$str);
  466.         return $str;
  467.     }
  468.     /**
  469.      * This is shorthand for print $this->subst($varname). See subst for further     details.
  470.      * USAGE: psubst(string $varname)
  471.      * @access public
  472.      * @param string $varname 
  473.      * @return bool 
  474.      */
  475.     public function psubst($varname)
  476.     {
  477.         print $this->subst($varname);
  478.         return false;
  479.     }
  480.     /**
  481.      * Substitutes the values of all defined variables in the variable
  482.      * named $varname and stores or appends the result in the variable named $target.
  483.      * It may be called with either a target and a varname as two strings or a
  484.      * target as a string and an array of variable names in varname.
  485.      * The public function inserts the new value of the variable into the $varkeys and
  486.      * $varvals hashes. It is not necessary for a variable to exist in these hashes
  487.      * before calling this public function.
  488.      * An optional third parameter allows the value for each varname to be appended
  489.      * to the existing target variable instead of replacing it. The default is to
  490.      * replace.
  491.      * If $target and $varname are both strings, the substituted value of the
  492.      * variable $varname is inserted into or appended to $target.
  493.      * If $handle is an array of variable names the variables named by $handle are
  494.      * sequentially substituted and the result of each substitution step is
  495.      * inserted into or appended to in $target. The resulting substitution is
  496.      * available in the variable named by $target, as is each intermediate step
  497.      * for the next $varname in sequence. Note that while it is possible, it
  498.      * is only rarely desirable to call this public function with an array of varnames
  499.      * and with $append = true.
  500.      * USAGE: parse(string $target, string $varname, [boolean $append])
  501.      * or
  502.      * USAGE: parse(string $target, array $varname = (string $varname), [boolean $append])
  503.      * @access public
  504.      * @param string $target 
  505.      * @param mixed $varname 
  506.      * @param bool $append 
  507.      * @return mixed 
  508.      */
  509.     public function parse($target$varname$append false)
  510.     {
  511.         if (!is_array($varname))
  512.         {
  513.             $str $this->subst($varname);
  514.             if ($append)
  515.             {
  516.                 $this->setVar($target$this->getVar($target$str);
  517.             }
  518.             else  
  519.             {
  520.                 $this->setVar($target$str);
  521.             }
  522.         }
  523.         else
  524.         {
  525.             reset($varname);
  526.             while(list($i$veach($varname))
  527.             {
  528.                 $str $this->subst($v);
  529.                 if ($append)
  530.                 {
  531.                     $this->setVar($target$this->getVar($target$str);
  532.                 }
  533.                 else  
  534.                 {
  535.                     $this->setVar($target$str);
  536.                 }
  537.             }
  538.         }
  539.         if(isset($this->start_time[$varname]&& $varname===strtolower($varname))
  540.         {
  541.             $this->endTime[$varname=$this->endTimer($varname);
  542.         }
  543.         return $this->getVar($target);
  544.     }
  545.     /**
  546.      * This is shorthand for print $this->parse(...) and is public functionally identical.
  547.      * USAGE: pparse(string $target, string $varname, [boolean $append])
  548.      * or
  549.      * USAGE: pparse(string $target, array $varname = (string $varname), [boolean $append])
  550.      * @access public
  551.      * @param string $target 
  552.      * @param mixed $varname 
  553.      * @param bool $append 
  554.      * @return bool FALSE
  555.      */
  556.     public function pparse($target$varname$append false)
  557.     {
  558.         if($target=='OUTPUT' && $this->debugBlock==true)
  559.         {
  560.             $totalTimeBlock 0;
  561.             foreach($this->start_time as $ky => $val)
  562.             {
  563.                 $totalTimeBlock +=$this->endTime[$ky];
  564.                 printf("<b>block:</b>  <b>%s</b> = '%f'<br>\n"$ky,$this->endTime[$ky]);
  565.             }
  566.             printf("<b>Total time for blocks:</b>  '%f'<br>\n"$totalTimeBlock);
  567.         }
  568.         print $this->finish($this->parse($target$varname$append));
  569.         return false;
  570.     }
  571.     /**
  572.      * Returns an associative array of all defined variables with the
  573.      * name as the key and the value of the variable as the value.
  574.      * This is mostly useful for debugging. Also note that $this->debug can be used
  575.      * to echo all variable assignments as they occur and to trace execution.
  576.      * USAGE: getVars()
  577.      * @access public
  578.      * @return array 
  579.      */
  580.     public function getVars()
  581.     {
  582.         reset($this->varkeys);
  583.         while(list($k$veach($this->varkeys))
  584.         {
  585.             $result[$k$this->getVar($k);
  586.         }
  587.         return $result;
  588.     }
  589.     /**
  590.      * Returns the value of the variable named by $varname.
  591.      * If $varname references a file and that file has not been loaded yet, the
  592.      * variable will be reported as empty.
  593.      * When called with an array of variable names this public function will return a a
  594.      * hash of variable values keyed by their names.
  595.      * USAGE: getVar(string $varname)
  596.      * or
  597.      * USAGE: getVar(array $varname)
  598.      * @access public
  599.      * @param mixed $varname 
  600.      * @return mixed 
  601.      */
  602.     public function getVar($varname)
  603.     {
  604.         if (!is_array($varname))
  605.         {
  606.             $str (isset($this->varvals[$varname]))?  $this->varvals[$varname]'';
  607.             if ($this->debug 2)
  608.             {
  609.                 printf ("<b>getVar</b> (with scalar) <b>%s</b> = '%s'<br>\n"$varnamehtmlentities($str));
  610.             }
  611.             return $str;
  612.         }
  613.         else
  614.         {
  615.             reset($varname);
  616.             while(list($k$veach($varname))
  617.             {
  618.                 $str (isset($this->varvals[$v])) ?  $this->varvals[$v]'';
  619.                 if ($this->debug 2)
  620.                 {
  621.                     printf ("<b>getVar:</b> (with array) <b>%s</b> = '%s'<br>\n"$vhtmlentities($str));
  622.                 }
  623.                 $result[$v$str;
  624.             }
  625.             return $result;
  626.         }
  627.     }
  628.     /**
  629.      * Returns a hash of unresolved variable names in $varname, keyed
  630.      * by their names (that is, the hash has the form $a[$name] = $name).
  631.      * USAGE: getUndefined(string $varname)
  632.      * @access public
  633.      * @param string $varname 
  634.      * @return array or false
  635.      */
  636.     public function getUndefined($varname)
  637.     {
  638.         if (!$this->loadFile($varname))
  639.         {
  640.             $this->halt('getUndefined: unable to load '.$varname);
  641.             return false;
  642.         }
  643.         preg_match_all(
  644.         (('loose' == $this->unknownRegexp"/{([^ \t\r\n}]+)}/" "/{([_a-zA-Z]\\w+)}/"),
  645.         $this->getVar($varname),
  646.         $m);
  647.         $m $m[1];
  648.         if (!is_array($m))  return false;
  649.  
  650.         reset($m);
  651.         while(list($k$veach($m))
  652.         {
  653.             if (!isset($this->varkeys[$v]))
  654.             {
  655.                 $result[$v$v;
  656.             }
  657.         }
  658.         if (isset($result&& count($result))
  659.         {
  660.             return $result;
  661.         }
  662.         else
  663.         {
  664.             return false;
  665.         }
  666.     }
  667.     /**
  668.      * Returns the finished version of $str.
  669.      * USAGE: finish(string $str)
  670.      * @access public
  671.      * @param string $str 
  672.      * @return string 
  673.      */
  674.     public function finish($str)
  675.     {
  676.         switch ($this->unknowns)
  677.         {
  678.             case 'keep':
  679.             break;
  680.  
  681.             case 'remove':
  682.                 $str preg_replace(
  683.                 (('loose' == $this->unknownRegexp"/{([^ \t\r\n}]+)}/" "/{([_a-zA-Z]\\w+)}/"),
  684.                 "",
  685.                 $str);
  686.             break;
  687.  
  688.             case 'comment':
  689.                 $str preg_replace(
  690.                      (('loose' == $this->unknownRegexp"/{([^ \t\r\n}]+)}/" "/{([_a-zA-Z]\\w+)}/"),
  691.                     "<!-- Template variable \\1 undefined -->",
  692.                     $str);
  693.             break;
  694.         }
  695.         return $str;
  696.     }
  697.     /**
  698.      * Prints the finished version of the value of the variable named by $varname.
  699.      * USAGE: p(string $varname)
  700.      * @access public
  701.      * @param string $varname 
  702.      * @return void 
  703.      */
  704.     public function p($varname)
  705.     {
  706.         print $this->finish($this->getVar($varname));
  707.     }
  708.     /**
  709.      * Returns the finished version of the value of the variable named  by $varname.
  710.      * USAGE: get(string $varname)
  711.      * @access public
  712.      * @param string $varname 
  713.      * @return void 
  714.      */
  715.     public function get($varname)
  716.     {
  717.         return $this->finish($this->getVar($varname));
  718.     }
  719.     /**
  720.      * When called with a relative pathname, this function will return the pathname
  721.      * with $this->root prepended. Absolute pathnames are returned unchanged.
  722.      * RETURNS: a string containing an absolute pathname.
  723.      * USAGE: filename(string $filename)
  724.      * @access private
  725.      * @param string $filename 
  726.      * @return string 
  727.      * @see set_root
  728.      */
  729.     private function filename($filename)
  730.     {
  731.         if (substr($filename01!= '/'
  732.                 && substr($filename01!= '\\'
  733.                 && substr($filename12!= ':\\'
  734.                 && substr($filename12!= ':/'
  735.                 )
  736.         {
  737.             $filename $this->root.'/'.$filename;
  738.         }
  739.         if (!file_exists($filename))
  740.         {
  741.             $this->halt('filename: file '.$filename.' does not exist.');
  742.         }
  743.         if (is_array($this->fileFallbacks&& count($this->fileFallbacks0)
  744.         {
  745.             reset($this->fileFallbacks);
  746.             while (list(,$veach($this->fileFallbacks))
  747.             {
  748.                 if (file_exists($v.basename($filename)))
  749.                 {
  750.                     return $v.basename($filename);
  751.                 }
  752.             }
  753.             $this->halt(sprintf('filename: file %s does not exist in the fallback paths %s.',$filenameimplode(','$this->fileFallbacks)));
  754.             return false;
  755.         }
  756.         return $filename;
  757.     }
  758.     /**
  759.      * Will construct a regexp for a given variable name with any  special
  760.      * chars quoted.
  761.      * @access private
  762.      * @param string $varname 
  763.      * @return string 
  764.      */
  765.     private function varname($varname)
  766.     {
  767.         return preg_quote('{'.$varname.'}');
  768.     }
  769.     /**
  770.      * If a variable's value is undefined and the variable has a filename stored in
  771.      * $this->file[$varname] then the backing file will be loaded and the file's
  772.      * contents will be assigned as the variable's value.
  773.      * USAGE: loadFile(string $varname)
  774.      * @access private
  775.      * @param string $varname 
  776.      * @return bool 
  777.      */
  778.     private function loadFile($varname)
  779.     {
  780.         if (!isset($this->file[$varname]))
  781.         {
  782.             // $varname does not reference a file so return
  783.             return true;
  784.         }
  785.         if (isset($this->varvals[$varname]))
  786.         {
  787.             // will only be unset if varname was created with setFile and has never been loaded
  788.             // $varname has already been loaded so return
  789.             return true;
  790.         }
  791.         $filename $this->file[$varname];
  792.         //  use @file here to avoid leaking filesystem information if there is an error
  793.         $str implode(''@file($filename));
  794.         if (empty($str))
  795.         {
  796.             $this->halt('loadFile: While loading $varname, '.$filename.' does not exist or is empty.');
  797.             return false;
  798.         }
  799.         if ($this->filenameComments)
  800.         {
  801.             $str "<!-- START FILE ".$filename." -->\n$str<!-- END FILE ".$filename." -->\n";
  802.         }
  803.         $this->setVar($varname$str);
  804.         return true;
  805.     }
  806.     /**
  807.      * Is called whenever an error occurs and will handle the error according
  808.      * to the  policy defined in $this->haltOnError. Additionally the    error message will be saved
  809.      * in $this->lastError.
  810.      * USAGE: halt(string $msg)
  811.      * @access private
  812.      * @param string $msg 
  813.      * @return bool 
  814.      */
  815.     private function halt($msg)
  816.     {
  817.         $this->lastError = $msg;
  818.         if ($this->haltOnError != 'no')
  819.         {
  820.             $this->haltMsg($msg);
  821.         }
  822.         if ($this->haltOnError == 'yes')
  823.         {
  824.             die('<b>Halted.</b>');
  825.         }
  826.         return false;
  827.     }
  828.     /**
  829.      * Prints an error message.
  830.      * It can be overridden by your subclass of Template. It will be called with an
  831.      * error message to display.
  832.      * USAGE: haltMsg(string $msg)
  833.      * @access private
  834.      * @param string $msg 
  835.      * @return void 
  836.      */
  837.     private function haltMsg($msg)
  838.     {
  839.         $the_error ='';
  840.         $the_error .= "\n\n ".$msg."\n\n";
  841.         $the_error .= "Date: ".date("l dS of F Y h:i:s A");
  842.         
  843.         $out "<html><head><title>Template Error</title>
  844.                 <style>P,BODY{ font-family: trebuchet MS,sans-serif; font-size:11px;
  845.                     }</style></head><body>&nbsp;<br><br><blockquote><b>There is an error with the
  846.                     template system.</b><br><b>Error Returned: </b><br>
  847.             <form name='mysql'><textarea rows=\"5\" cols=\"60\">".htmlspecialchars($the_error)."</textarea></form></blockquote></body></html>";
  848.         if(APPLICATION_ENV != 'production'print $out;
  849.         exit();
  850.     }
  851.     /**
  852.      * Returns the last error message if any
  853.      * @access public
  854.      * @return boolean|stringLast error message if any
  855.      */
  856.     public function getLastError()
  857.     {
  858.         if ($this->lastError == '')
  859.         {
  860.             return false;
  861.         }
  862.         return $this->lastError;
  863.     }
  864.     /**
  865.      * Initialize the value of a variable.
  866.      * It may be called with either a varname and a value as two strings or an
  867.      * an associative array with the key being the varname and the value being
  868.      * the new variable value.
  869.      * The public function inserts the new value of the variable into the $varkeys and
  870.      * $varvals hashes. It is not necessary for a variable to exist in these hashes
  871.      * before calling this public function.
  872.      * An optional third parameter allows the value for each varname to be appended
  873.      * to the existing variable instead of replacing it. The default is to replace.
  874.      * USAGE: initVar(string $varname, [string $value = ''], [boolean $append = false])
  875.      * or
  876.      * USAGE: initVar(array $varname = (string $varname => string $value),
  877.      * [mixed $dummy_var], [boolean $append = false])
  878.      * or
  879.      * USAGE: initVar(array $varname = (string $value),
  880.      * [mixed $dummy_var], [boolean $append = false])
  881.      * @access public
  882.      * @param mixed $varname 
  883.      * @param mixed $value 
  884.      * @param bool $append 
  885.      * @return void 
  886.      */
  887.     public function initVar($varname$value ''$append false)
  888.     {
  889.         if (!is_array($varname))
  890.         {
  891.             if (!empty($varname))
  892.             {
  893.                 $this->setVar($varname$value$append);
  894.             }
  895.         }
  896.         else
  897.         {
  898.             reset($varname);
  899.             while(list($k$veach($varname))
  900.             {
  901.                 if(is_int($k))
  902.                 {                    
  903.                     $this->setVar($v$value$append);
  904.                 }
  905.                 elseif(is_string($k))
  906.                 {
  907.                     $this->setVar($k$v$append);
  908.                 }
  909.                 elseif ($this->debug 1)
  910.                 {
  911.                     printf("<b>initVar:</b> (with scalar) <b>%s</b> = '%s'<br>\n"$varnamehtmlentities($value));
  912.                 }
  913.             }
  914.         }
  915.     }
  916.     /**
  917.      * Initialize a block
  918.      * A variable $parent may contain a variable block defined by:
  919.      * &lt;!-- BEGIN $varname --&gt; content &lt;!-- END $varname --&gt;.
  920.      * This public function removes that block from $parent and replaces it
  921.      * with a variable reference named $name.
  922.      * The block is inserted into the varkeys and varvals hashes. If $name is
  923.      * omitted, it is assumed to be the same as $varname.
  924.      * Blocks may be nested but care must be taken to extract the blocks in order
  925.      * from the innermost block to the outermost block.
  926.      * USAGE: setBlock(string $parent, string $varname, [string $name = ''])
  927.      * @access public
  928.      * @param mixed $target 
  929.      * @param mixed $value 
  930.      * @param string $name 
  931.      * @return bool 
  932.      */
  933.     public function initBlock($target$value ''$append false)
  934.     {
  935.         if(!is_array($target))
  936.         {
  937.             $this->parse($target$value$append);
  938.         }
  939.         
  940.         else
  941.         {            
  942.             reset($target);            
  943.             while(list($k$veach($target))
  944.             {
  945.                 if(is_int($k))
  946.                 {                
  947.                     $this->parse($v$value$append);
  948.                 }
  949.                 elseif(is_string($k))
  950.                 {
  951.                     $this->parse($k$v$append);
  952.                 }
  953.                 else  $this->setVar($target$str);
  954.             }
  955.         }
  956.     }
  957. }

Documentation generated on Wed, 21 Jul 2010 07:34:44 +0000 by phpDocumentor 1.4.3