Source for file yapter.php

Documentation is available at yapter.php

  1. <?php
  2. /**
  3.  * Yapter - Yet Another PHP Template Engine
  4.  *
  5.  * @package Yapter
  6.  * @version 3.0.0
  7.  * @copyright Copyright 2001-2006 Vincent Driessen
  8.  * @copyright Copyright 2006-2007 Kevin L. Papendick
  9.  * @author Vincent Driessen <nvie@users.sourceforge.net>
  10.  * @author Kevin L. Papendick <kevinp@polarlava.com>
  11.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  12.  * @filesource
  13.  */
  14.  
  15. /**
  16.  * Yapter warning level constant - Silent
  17.  * @see Yapter::setWarningLevel()
  18.  */
  19. define('E_YAPTER_NONE',            0);
  20.  
  21. /**
  22.  * Yapter warning level constant - Report notices
  23.  * @see Yapter::setWarningLevel()
  24.  */
  25. define('E_YAPTER_NOTICE',          1);
  26.  
  27. /**
  28.  * Yapter warning level constant - Report warnings
  29.  * @see Yapter::setWarningLevel()
  30.  */
  31. define('E_YAPTER_WARNING',         2);
  32.  
  33. /**
  34.  * Yapter warning level constant - Report errors
  35.  * @see Yapter::setWarningLevel()
  36.  */
  37. define('E_YAPTER_ERROR',           4);
  38.  
  39. /**
  40.  * Yapter warning level constant - Die on errors
  41.  * @see Yapter::setWarningLevel()
  42.  */
  43. define('E_YAPTER_DIE_ON_ERROR',    8);
  44.  
  45. /**
  46.  * Yapter warning level constant - All: Report errors, warnings and notices
  47.  * @see Yapter::setWarningLevel()
  48.  */
  49. define('E_YAPTER_ALL',             15);
  50.  
  51.  
  52. /**
  53.  * Yapter - Yet Another PHP Template Engine
  54.  *
  55.  * @package Yapter
  56.  * @since V3.0.0
  57.  */
  58. class Yapter {
  59.  
  60.     /**
  61.      * Document Root
  62.      *
  63.      * @var string 
  64.      */
  65.     var $_ROOT;
  66.  
  67.     /**
  68.      * Ignore unknown variables?
  69.      *
  70.      * @var boolean 
  71.      */
  72.     var $ignore_unknown_vars;
  73.  
  74.     /**
  75.      * Automatically hide unparsed blocks?
  76.      *
  77.      * @var boolean 
  78.      */
  79.     var $hide_unparsed_blocks;
  80.  
  81.     /**
  82.      * Template content blocks
  83.      *
  84.      * $blox[blockname]['content']  holds the template's content
  85.      * $blox[blockname]['numlines'] holds the number of lines in the block
  86.      * $blox[blockname]['parsed']   holds an array with the parsed data
  87.      * $blox[$_ROOT]                always holds the main template
  88.      * @var array 
  89.      */
  90.     var $blox = array();
  91.  
  92.     /**
  93.      * Tracks all block definitions from which multiple block instances can be created.
  94.      *
  95.      * @var array 
  96.      */
  97.     var $blockDefs = array();
  98.  
  99.     /**
  100.      * Array of variables available to all blocks
  101.      *
  102.      * @var array 
  103.      */
  104.     var $vars;
  105.  
  106.     /**
  107.      * Warning level verbosity
  108.      *
  109.      * @see setWarningLevel()
  110.      * @var string 
  111.      */
  112.     var $warning_level;
  113.  
  114.     /**
  115.      * Script start time used to calculate debug execution time.
  116.      *
  117.      * @var float 
  118.      */
  119.     var $startTime;
  120.  
  121.     /**
  122.      * Declared variables that were not set.
  123.      *
  124.      * @var array 
  125.      */
  126.     var $missing_var_list;
  127.  
  128.  
  129.  
  130.     /**
  131.      * Yapter Constructor
  132.      *
  133.      * @param string $file The fully pathed template file
  134.      * @param int $level The warning level
  135.      */
  136.     function Yapter($file$level E_YAPTER_ALL)
  137.     {
  138.         $this->setWarningLevel((int) $level);
  139.         $this->startTime = $this->getmicrotime();
  140.         $this->setIgnoreUnknownVars(FALSE);
  141.         $this->setHideUnparsedBlocks(FALSE);
  142.         $this->addBlockFromFile('__DOCUMENT_ROOT'$file);
  143.         $this->vars = array();
  144.         $this->missing_var_list = array();
  145.     }
  146.  
  147.     /**
  148.      * Error handler
  149.      *
  150.      * @param int $line_num Line number.
  151.      * @param string $msg The error message.
  152.      */
  153.     function error($line_num$msg)
  154.     {
  155.         if ($this->getWarningLevel(E_YAPTER_ERROR{
  156.             trigger_error('[' date('Y-m-d H:i:s''] Yapter (Line ' $line_num '): ' $msgE_USER_ERROR);
  157.             if ($this->getWarningLevel(E_YAPTER_DIE_ON_ERROR{
  158.                 exit;
  159.             }
  160.         }
  161.     }
  162.  
  163.     /**
  164.      * Warning handler
  165.      *
  166.      * @param int $line_num Line number.
  167.      * @param string $msg The warning message.
  168.      */
  169.     function warning($line_num$msg)
  170.     {
  171.         if ($this->getWarningLevel(E_YAPTER_WARNING{
  172.             trigger_error('[' date('Y-m-d H:i:s''] Yapter (Line ' $line_num '): ' $msgE_USER_WARNING);
  173.         }
  174.     }
  175.  
  176.     /**
  177.      * Notice handler
  178.      *
  179.      * @param int $line_num Line number.
  180.      * @param string $msg The notice message.
  181.      */
  182.     function notice($line_num$msg)
  183.     {
  184.         if ($this->getWarningLevel(E_YAPTER_NOTICE{
  185.             trigger_error('[' date('Y-m-d H:i:s''] Yapter (Line ' $line_num '): ' $msgE_USER_NOTICE);
  186.         }
  187.     }
  188.  
  189.     /**
  190.      * Sets the level of verbosity of error/warning/notices that Yapter displays.
  191.      *
  192.      * @param int The warning level
  193.      */
  194.     function setWarningLevel($level)
  195.     {
  196.         $this->warning_level = $level;
  197.     }
  198.  
  199.     /**
  200.      * Gets the level of verbosity of error/warning/notices that Yapter displays.
  201.      */
  202.     function getWarningLevel()
  203.     {
  204.         return $this->warning_level;
  205.     }
  206.  
  207.     /**
  208.      * Sets weather to ignore unknown template variables.
  209.      *
  210.      * @param boolean $a 
  211.      */
  212.     function setIgnoreUnknownVars($a)
  213.     {
  214.         $this->ignore_unknown_vars = $a;
  215.     }
  216.  
  217.     /**
  218.      * Gets weather to ignore unknown template variables.
  219.      *
  220.      * @return boolean 
  221.      */
  222.     function getIgnoreUnknownVars()
  223.     {
  224.         return $this->ignore_unknown_vars;
  225.     }
  226.  
  227.     /**
  228.      * Sets weather to hide unparsed template blocks.
  229.      *
  230.      * @param boolean $a 
  231.      */
  232.     function setHideUnparsedBlocks($a)
  233.     {
  234.         $this->hide_unparsed_blocks = $a;
  235.     }
  236.  
  237.     /**
  238.      * Gets weather to hide unparsed template blocks.
  239.      *
  240.      * @return boolean 
  241.      */
  242.     function getHideUnparsedBlocks()
  243.     {
  244.         return $this->hide_unparsed_blocks;
  245.     }
  246.  
  247.     /**
  248.      * Issue warning the first time an assigned variable is found, but not set.
  249.      *
  250.      * @param string $varname The name of the unset template variable.
  251.      */
  252.     function warnVarNotSet($varname)
  253.     {
  254.         if (!in_array($varname$this->getMissingVarList())) {
  255.             $this->setMissingVar($varname);
  256.             $this->warning(__LINE__'Variable "' htmlspecialchars($varname'" found, but never assigned a value ');
  257.         }
  258.     }
  259.  
  260.     /**
  261.      * List of variables that were found to not be set during parsing.
  262.      *
  263.      * @param string $a The name of the unset template variable.
  264.      */
  265.     function setMissingVar($varname)
  266.     {
  267.         if (!is_array($this->missing_var_list)) {
  268.             $this->missing_var_list = array();
  269.         }
  270.         $this->missing_var_list[$varname;
  271.  
  272.         if (!in_array($varname$this->missing_var_list)) {
  273.             $this->missing_var_list[$varname;    // Add it to the list...
  274.             // ...and print a warning once.
  275.             $this->warning(__LINE__'Variable "' htmlspecialchars($varname'" found, but never assigned a value ');
  276.         }
  277.     }
  278.  
  279.     function getMissingVarList()
  280.     {
  281.         if (!is_array($this->missing_var_list)) {
  282.             $this->missing_var_list = array();
  283.         }
  284.         return $this->missing_var_list;
  285.     }
  286.  
  287.     /**
  288.      * Adds a new block to the blox-array
  289.      *
  290.      * @param string $blockname The name of the block.
  291.      * @param string $content The content of the block.
  292.      */
  293.     function addBlock($blockname$content)
  294.     {
  295.         $this->blox[$blockname]['content'$content;
  296.         $this->blox[$blockname]['numlines'count($this->blox[$blockname]['content']);
  297.         $this->blox[$blockname]['parsed''';
  298.         $this->prepare($blockname);
  299.     }
  300.  
  301.     /**
  302.      * Adds a new block, filling it with the specified's file contents
  303.      *
  304.      * @param string $blockname The name of the block.
  305.      * @param string $file The file containing the contents for the block.
  306.      */
  307.     function addBlockFromFile($blockname$file)
  308.     {
  309.         $content @file($fileor $this->error(__LINE__'Cannot open and read template file ' htmlspecialchars($file' for addBlockFromFile() ');
  310.         $this->addBlock($blockname$content);
  311.     }
  312.  
  313.     /**
  314.      * Adds a block definition to the block-definition array from which other blocks can be copied
  315.      * @param string $blockdef The name of an existing block.
  316.      * @param string $content The content of the block definition.
  317.      */
  318.     function addBlockDef($blockdef$content)
  319.     {
  320. /*
  321.         TODO: Should this be culled?  -klp
  322.         if (isset($this->blockDefs[$blockdef])) {
  323.             $this->error(__LINE__, 'Block "' . htmlspecialchars($blockdef) . '" already exist and cannot be redefine ');
  324.         } else {
  325.             $this->blockDefs[$blockdef] = $content;
  326.         }
  327. */
  328.         $this->blockDefs[$blockdef$content;
  329.     }
  330.  
  331.     /**
  332.      * Copies a block from the block definition array
  333.      * @param string $blockname The name of the block.
  334.      * @param string $blockdef The name of an existing defined block.
  335.      */
  336.     function addBlockFromDef($blockname$blockdef)
  337.     {
  338.         $this->addBlock($blockname$this->blockDefs[$blockdef]);
  339.     }
  340.  
  341.     /**
  342.      * Handles subprocessing of templates found in the main template file
  343.      *
  344.      * @param string $blockname The name of the block.
  345.      */
  346.     function prepare($blockname)
  347.     {
  348.         $currblockcontents array();
  349.         $block &$this->blox[$blockname];
  350.         for ($i 0$i $block['numlines']$i++{
  351.             if (isset($block['content'][$i])) {
  352.                 $line $block['content'][$i];
  353.             else {
  354.                 continue;
  355.             }
  356.  
  357.             // Try to find a tag-definition on this line
  358.             if (preg_match('/\[(INCLUDE|BLOCK|END|REUSE|SET) ([A-Za-z0-9_.\/-]+)( AS ([A-Za-z0-9_-]+))?]/'$line$matches)) {
  359.                 $type $matches[1];
  360.                 $name (!empty($matches[4])) $matches[4$matches[2];
  361.                 if ($type == 'END' && !isset($currblockdef)) {
  362.                     $this->error(__LINE__'"[END ' $name ']" found without matching "[BLOCK ' $name ']" or "[SET ' $name ']" ');
  363.                 }
  364.                 if ($type == 'END' && $matches[2== $currblockdef{
  365.                     if (isset($matches[4])) {
  366.                         $this->error(__LINE__'Given "AS" parameter not allowed in END tags ');
  367.                     }
  368.  
  369.                     // End the current block definition: add the block to the blox-array
  370.                     if (isset($currblockdef&& isset($currblockcontents&& isset($currblockname)) {
  371.                         $this->addBlockDef($currblockdef$currblockcontents);
  372.                         $this->addBlockFromDef($currblockname$currblockdef);
  373.                     }
  374.  
  375.                     // Now, try to remove the block from the template definition, replacing it with a var
  376.                     for ($j $i$j >= $currblockstart$j--{
  377.                         if ($j == $currblockstart && $currblocktype == 'BLOCK'{
  378.                             $block['content'][$j"{" .  $currblockname "}";
  379.                         else {
  380.                             unset($block['content'][$j]);
  381.                         }
  382.                     }
  383.  
  384.                     // Reset for further preparing.
  385.                     unset($currblocktype);
  386.                     unset($currblockstart);
  387.                     unset($currblockname);
  388.                     unset($currblockdef);
  389.                     $currblockcontents array();
  390.  
  391.                 elseif (($type == 'SET' || $type == 'BLOCK'&& !isset($currblockname)) {
  392.                     if ($type == 'BLOCK'{
  393.                         // Start BLOCK definition
  394.                         $currblocktype  $type;
  395.                         $currblockstart $i;
  396.                         $currblockname  $name;
  397.                         $currblockdef   $matches[2];
  398.                     else {
  399.                         // Start SET definition
  400.                         if (isset($matches[4])) {
  401.                             $this->error(__LINE__'Given "AS" parameter not allowed in SET tags ');
  402.                         }
  403.                         $currblocktype  $type;
  404.                         $currblockstart $i;
  405.                         $currblockname  $matches[2];
  406.                         $currblockdef   $matches[2];
  407.  
  408.                     }
  409.  
  410.                 elseif ($type == 'INCLUDE' && !isset($currblockname)) {
  411.                     // Make this line a variable...
  412.                     $block['content'][$i'{' $name .  "}\n";
  413.  
  414.                     // ...and include the given file...
  415.                     $this->addBlockFromFile($name$matches[2]);
  416.  
  417.                 elseif ($type == 'REUSE' && !isset($currblockname)) {
  418.  
  419.                     if (!isset($matches[4])) {
  420.                         $this->error(__LINE__'Missing required "AS" parameter in [REUSE ' $name '] tag ');
  421.                     }
  422.  
  423.                     // Make this line a variable...
  424.                     $block['content'][$i'{' .  $matches[4"}\n";
  425.  
  426.                     // ...and get this REUSE value from the block definition list...
  427.                     $this->addBlockFromDef($matches[4]$matches[2]);
  428.  
  429.                 elseif ($currblockname != $name{
  430.                     if ($currblockname{
  431.                         $currblockcontents[$line;
  432.                     }
  433.                 }
  434.  
  435.             else {
  436.                 // No tag-definition... just normal text so do nothing here
  437.                 if (!empty($currblockname)) {
  438.                     $currblockcontents[$line;
  439.                 }
  440.             }
  441.         }
  442.     }
  443.  
  444.     /**
  445.      * Parses the specified block, filling variables and nested blockdefs
  446.      *
  447.      * @param string $blockname The name of the block.
  448.      * @return array $this->blox[$blockname]['parsed'] Parsed block
  449.      */
  450.     function parse($blockname '')
  451.     {
  452.         if (!$blockname{
  453.             $blockname $this->_ROOT;
  454.         }
  455.         if (!isset($this->blox[$blockname])) {
  456.             $this->error(__LINE__'Unable to parse block because block "' htmlspecialchars($blockname'" does not exist ');
  457.         }
  458.  
  459.         $block &$this->blox[$blockname];
  460.         $parsed $block['content'];
  461.  
  462.         // Loop through all the lines of the template and parse variables one-by-one
  463.         for ($i 0$i $block['numlines']$i++{
  464.             if (!isset($parsed[$i])) {
  465.                 continue;
  466.             }
  467.             $line $parsed[$i];
  468.  
  469.             // Look for variables in this line, processing it character-by-character
  470.             unset($start);
  471.             unset($buffer);
  472.             for ($j 0$j strlen($line)$j++{
  473.                 $char $line[$j];
  474.                 if (!isset($start&& $char == '{'{
  475.                     $start $j;
  476.                 elseif (isset($start&& $char == '}'{
  477.                     // The sequence {} is not a valid variable value
  478.                     if (!isset($buffer)) {
  479.                         unset($start);
  480.                         continue;
  481.                     else {
  482.                         // Gotcha! Now replace this variable with its contents
  483.                         // First, check to see if it's a variable or a block that has to be parsed
  484.                         if (isset($this->vars[$buffer])) {
  485.                             $value $this->vars[$buffer];
  486.                         elseif (isset($this->blox[$buffer])) {
  487.                             if ($this->blox[$buffer]['parsed']{
  488.                                 // The value must be filled with the parsed data from the $buffer block
  489.                                 $value @implode(''$this->blox[$buffer]['parsed']);
  490.                             elseif ($this->getWarningLevel($this->getHideUnparsedBlocks()) {
  491.                                 // Automaticly hide unparsed bloks
  492.                                 $value "";
  493.                             else {
  494.                                 // Make the recursive call now
  495.                                 $value @implode(''$this->parse($buffer));
  496.                             }
  497.                         else {
  498.                             // No variable or block name found by the name of $buffer
  499.                             if ($this->getIgnoreUnknownVars()) {
  500.                                 // Ignore, replace with an empty string.
  501.                                 $value '';
  502.                             else {
  503.                                 // Warn and continue.
  504.                                 $this->warnVarNotSet($buffer);
  505.                                 unset($start);
  506.                                 unset($buffer);
  507.                                 continue;
  508.                             }
  509.                         }
  510.                         $part1 substr($line0$start);
  511.                         $part2 substr($line$start strlen($buffer2);
  512.                         $line $part1 $value $part2;
  513.                         $j += strlen($value(strlen($buffer2);
  514.                         unset($start);
  515.                         unset($buffer);
  516.                     }
  517.                 elseif (isset($start)) {
  518.                     // Check to see $char is a proper character (range: [A-Za-z0-9_./-])
  519.                     // In Yapter 2.13b2, I've added the '/' char as well, to support inclusion
  520.                     // from Unix paths, like '../../foo.tpl'
  521.                     if (($char >= 'a' && $char <= 'z'|| ($char >= '0' && $char <= '9'|| ($char >= 'A' && $char <= 'Z'|| ($char == '_'|| ($char == '.'|| ($char == '-'|| ($char == '/')) {
  522.                         if (!empty($buffer)) {
  523.                             $buffer .= $char;
  524.                         else {
  525.                             $buffer $char;
  526.                         }
  527.                     else {
  528.                         unset($start);
  529.                         unset($buffer);
  530.                     }
  531.                 }
  532.             }
  533.             $parsed[$i$line;
  534.         }
  535.  
  536.         if (is_array($this->blox[$blockname]['parsed'])) {
  537.             $this->blox[$blockname]['parsed'array_merge($this->blox[$blockname]['parsed']$parsed);
  538.         else {
  539.             $this->blox[$blockname]['parsed'= (array) $parsed;
  540.         }
  541.  
  542.         return $this->blox[$blockname]['parsed'];
  543.     }
  544.  
  545.     /**
  546.      * Assigns a value to a variable ({varname}).
  547.      * @param string $varname Template variable name.
  548.      * @param string|int$value the value of the template variable.
  549.      */
  550.     function set($varname$value)
  551.     {
  552.         if (isset($value)) {
  553.             $this->vars[$varname$value;
  554.         else {
  555.             $this->warning(__LINE__'Trying to set "' htmlspecialchars($varname'" to NULL. Variable not set ');
  556.         }
  557.     }
  558.  
  559.     /**
  560.      * Assigns values to multiple variables
  561.      *
  562.      * @param array $variables Associative array (var_name => var_value) of template variables.
  563.      */
  564.     function setVars($variables)
  565.     {
  566.         if (!is_array($variables)) {
  567.             $this->error('Value passed to setVars() is not an array ');
  568.         }
  569.         foreach($variables as $varname => $value{
  570.             $this->set($varname$value);
  571.         }
  572.     }
  573.  
  574.     /**
  575.      * Assigns the contents of a file to a variable ({varname}).
  576.      *
  577.      * @param string $varname Template variable name.
  578.      * @param string $file The file containing the contents for the variable.
  579.      */
  580.     function setFile($varname$filename)
  581.     {
  582.         if (!file_exists($filename|| !is_readable($filename)) {
  583.             $this->error(__LINE__'The file "' htmlspecialchars($filename'" required to set the "' htmlspecialchars($varname'" variable does not exits or is unreadable ');
  584.         }
  585.         $value implode(''file($filename));
  586.         $this->set($varname$value);
  587.     }
  588.  
  589.     /**
  590.      * Gets the value of a variable.
  591.      *
  592.      * @param string $varname Template variable name.
  593.      * @return string|int|booleanThe value of the variable or FALSE if unset.
  594.      */
  595.     function getVar($varname)
  596.     {
  597.         if ($this->vars[$varname]{
  598.             return $this->vars[$varname];
  599.         else {
  600.             return FALSE;
  601.         }
  602.     }
  603.  
  604.     /**
  605.      * Gets the content of a block.
  606.      *
  607.      * @param string $blockname The name of the block.
  608.      * @return string|booleanThe contents of the block or FALSE if unset.
  609.      */
  610.     function getBlockContent($blockname)
  611.     {
  612.         if ($this->$blox[$blockname]['content']{
  613.             return @implode(''$this->$blox[$blockname]['content']);
  614.         else {
  615.             return FALSE;
  616.         }
  617.     }
  618.  
  619.     /**
  620.      * Replaces the contents of one block with the contents of another.
  621.      *
  622.      * @param string $blockname The name of the block to be replaced.
  623.      * @param string $replace_by_block The name of the block to duplicate.
  624.      */
  625.     function replace($block$replace_by_block)
  626.     {
  627.         if (!isset($this->blox[$block])) {
  628.             $this->error(__LINE__'Block "'  htmlspecialchars($block'" does not exist so it cannot be replaced by the "' htmlspecialchars($replace_by_block'" block ');
  629.         }
  630.         if (!isset($this->blox[$replace_by_block])) {
  631.             $this->error(__LINE__'Block "'  htmlspecialchars($replace_by_block'" does not exist so it cannot be used to replace the "' htmlspecialchars($block'" block ');
  632.         }
  633.         $this->blox[$block]['content'$this->blox[$replace_by_block]['content'];
  634.         $this->blox[$block]['numlines'$this->blox[$replace_by_block]['numlines'];
  635.     }
  636.  
  637.     /**
  638.      * Hides all the contents of the specified block.
  639.      *
  640.      * @param string $block The name of the block.
  641.      */
  642.     function hide($block)
  643.     {
  644.         if (!isset($this->blox[$block])) {
  645.             $this->error(__LINE__'Block "' htmlspecialchars($block'" does not exist so it cannot be hidden ');
  646.         }
  647.         $this->blox[$block]['content'array();
  648.         $this->blox[$block]['numlines'0;
  649.     }
  650.  
  651.     /**
  652.      * Resets the parsed data to an empty string and sets the block as "unparsed".
  653.      *
  654.      * @param string $blockname The name of the block.
  655.      */
  656.     function clear($blockname)
  657.     {
  658.         if (!isset($this->blox[$blockname])) {
  659.             $this->error(__LINE__'Block "'.htmlspecialchars($blockname).'" does not exist so it cannot be cleared ');
  660.         }
  661.         $this->blox[$blockname]['parsed''';
  662.  
  663.         // often, a variable is set whenever a block should be discarded...
  664.         // now reset such a variable to make sure the block is not overriden
  665.         unset($this->vars[$blockname]);
  666.     }
  667.  
  668.     /**
  669.      * Gets the parsed content of a block or the ROOT (complete) template.
  670.      *
  671.       * @param string $blockname The name of the block.
  672.       * @return string|booleanThe parsed contents of the block or FALSE if unset.
  673.      */
  674.     function getContents($blockname '')
  675.     {
  676.         if ($blockname == ''{
  677.             $blockname $this->_ROOT;
  678.         }
  679.         $parsed $this->blox[$blockname]['parsed'];
  680.         if ($parsed{
  681.             return implode(''$parsed);
  682.         else {
  683.             return FALSE;
  684.         }
  685.     }
  686.  
  687.     /**
  688.      * Prints the full template contents.
  689.       */
  690.     function spit()
  691.     {
  692.         echo $this->getContents();
  693.     }
  694.  
  695.     /**
  696.      * Get the Unix timestamp in seconds.
  697.      *
  698.      * @link http://www.php.net/manual/en/function.microtime.php
  699.      * @return float 
  700.      */
  701.     function getmicrotime()
  702.     {
  703.         if (strtoupper(substr(PHP_OS03)) === 'WIN'{
  704.             return 0.0;
  705.         else {
  706.             list($usec$secexplode(' 'microtime());
  707.             return ((float)$usec + (float)$sec);
  708.         }
  709.     }
  710.  
  711.     /**
  712.      * Gets the time in seconds required to parse the template.
  713.      *
  714.      * @return float 
  715.      */
  716.     function execTime()
  717.     {
  718.         return round($this->getmicrotime($this->startTime5);
  719.     }
  720.  
  721.     /**
  722.      * Gets the debug string with the time required to parse the template.
  723.      *
  724.      * @return string 
  725.      */
  726.     function executionTime()
  727.     {
  728.         echo "<p>\n\nThe execution time is <b>".$this->execTime()."</b> seconds.<br>\n";
  729.     }
  730. }
  731.  
  732.  
  733.  
  734. /****************
  735.  * Legacy Support *
  736.  ******************/
  737.  
  738.  
  739.  
  740. /**
  741.  * Yapter constant - Ignore unknown variables
  742.  * @deprecated Use Yapter::setIgnoreUnknownVars()
  743.  * @see Yapter::setIgnoreUnknownVars()
  744. */
  745. define('E_YAPTER_IGN_UNKNOWN_VARS',    16);
  746.  
  747. /**
  748.  * Yapter constant - Automaticly hide unparsed blocks
  749.  * @deprecated Use Yapter::setHideUnparsedBlocks()
  750.  * @see Yapter::setHideUnparsedBlocks()
  751.  */
  752. define('E_YAPTER_AUTO_HIDE_BLOCK',    32);
  753.  
  754.  
  755. /**
  756.  * Yapter - Yet Another PHP Template Engine (Legacy Class)
  757.  *
  758.  * @package Yapter
  759.  * @see Yapter class
  760.  * @deprecated This is for legacy support only, new developement should use Yapter class instead.
  761.  */
  762. class Template extends Yapter {
  763.  
  764.     /**
  765.      * Yapter Template Constructor
  766.      *
  767.      * @param string $file The fully pathed template file
  768.      * @param int|boolean$level The warning|(deprecated) debug level
  769.      * @see Yapter::setWarningLevel()
  770.       * @deprecated This is for legacy support only, new developement should use the Yapter class constructor.
  771.      */
  772.     function Template($file$level E_YAPTER_ALL)
  773.     {
  774.         if (is_bool($level)) {
  775.             //
  776.             // Rationale:
  777.             // =========
  778.             // Older Yapter versions had the possibility of turning
  779.             // on the so called "debug mode" with a bool parameter
  780.             // as the second argument to this constructor.
  781.             // However, since debug mode was dropped and the
  782.             // warning level support was built in, it was a logical
  783.             // step of replacing the second parameter.
  784.             //
  785.             // However, to prevent people from making mistakes,
  786.             // we'll check if the user passed a boolean parameter.
  787.             // If so, he or she is probably using debug mode, and
  788.             // we'll issue a notice in these cases.
  789.             //
  790.             // Thanks to Ivo Koster.
  791.             //
  792.             $level E_YAPTER_ALL;
  793.             $bad_debug TRUE;
  794.         }
  795.  
  796.         $this->Yapter($file$level);
  797.  
  798.         if (isset($bad_debug)) {
  799.             $this->notice(__LINE__'Debug mode has not been supported since V2.12, use setWaringLevel() instead ');
  800.         }
  801.  
  802.         //Support deprecated constants...
  803.         if ($this->getWarningLevel(E_YAPTER_IGN_UNKNOWN_VARS{
  804.             $this->setIgnoreUnknownVars(TRUE);
  805.         }
  806.         if ($this->getWarningLevel(E_YAPTER_AUTO_HIDE_BLOCK{
  807.             $this->setHideUnparsedBlocks(TRUE);
  808.         }
  809.     }
  810.  
  811.     /**
  812.      * Sets whether to parse unknown variables.
  813.      *
  814.      * @param boolean $a 
  815.      * @deprecated Use Yapter::setIgnoreUnknownVars()
  816.      * @see Yapter::setIgnoreUnknownVars()
  817.      */
  818.     function setParseMode($a)
  819.     {
  820.         $this->setIgnoreUnknownVars((bool) $a);
  821.     }
  822. }
  823. ?>

Documentation generated on Tue, 27 Feb 2007 22:01:39 -0500 by phpDocumentor 1.3.1