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

Source for file Generator.php

Documentation is available at Generator.php

  1. <?php
  2. require_once 'HTML/Template/PHPLIB/Helper.php';
  3.  
  4. /**
  5. * Generates code to be used with templates
  6. *
  7. @category HTML
  8. @package  HTML_Template_PHPLIB
  9. @author   Christian Weiske <cweiske@php.net>
  10. @license  http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  11. @link     http://pear.php.net/package/HTML_Template_PHPLIB
  12. */
  13. {
  14.  
  15.     /**
  16.     * Creates the code to use a given template file
  17.     *
  18.     * @param string $strFile    Template file
  19.     * @param string $strTplName Template reference name
  20.     * @param string $strPrefix  Prefix to prepend before the code
  21.     *
  22.     * @return string PHP code
  23.     */
  24.     function getCodeBlockDefinition($strFile$strTplName = null$strPrefix '$tpl')
  25.     {
  26.         $arBlocks HTML_Template_PHPLIB_Generator::getBlocks(
  27.             HTML_Template_PHPLIB_Helper::getLines($strFile)
  28.         );
  29.  
  30.         if ($strTplName === null{
  31.             $strTplName HTML_Template_PHPLIB_Generator::getTemplateNameFromFilename($strFile);
  32.         }
  33.  
  34.         $nl    "\r\n";
  35.         $code  '';
  36.         $code .= $strPrefix ' = new HTML_Template_PHPLIB();' $nl;
  37.         $code .= HTML_Template_PHPLIB_Generator::getCodeBlock($arBlocks$strTplName$strPrefix);
  38.         $code .= $nl;
  39.         $code .= '//TODO: do something with the code' $nl;
  40.         $code .= $nl;
  41.  
  42.         $code .= $strPrefix '->finish('
  43.                 . trim($strPrefix"->parse('TMP', '" $strTplName "'));" $nl;
  44.  
  45.         return $code;
  46.     }//function getCodeBlockDefinition($strFile, $strTplName = null, $strPrefix = '$tpl')
  47.  
  48.  
  49.  
  50.     /**
  51.     * Creates the PHP code for the given array of blocks.
  52.     *
  53.     * @param array  $arBlocks   Array of blocks, see getBlocks()
  54.     * @param string $strTplName Template reference name
  55.     * @param string $strPrefix  Prefix to prepend before the code
  56.     *
  57.     * @return string PHP code
  58.     */
  59.     function getCodeBlock($arBlocks$strTplName$strPrefix '$tpl')
  60.     {
  61.         $nl   "\r\n";
  62.         $code '';
  63.         foreach ($arBlocks as $arBlock{
  64.             if (count($arBlock['sub']> 0{
  65.                 $code .= HTML_Template_PHPLIB_Generator::getCodeBlock(
  66.                             $arBlock['sub']$strTplName$strPrefix
  67.                 );
  68.             }
  69.             $code .= $strPrefix "->setBlock('" $strTplName "','"
  70.                     . $arBlock['name'"', '"
  71.                     . $arBlock['name'"_ref');" $nl;
  72.         }
  73.  
  74.         return $code;
  75.     }//function getCodeBlock($arBlocks, $strTplName, $strPrefix = '$tpl')
  76.  
  77.  
  78.  
  79.     /**
  80.     * Returns an array of blocks in the given template code.
  81.     * The array values are array with a key "name" and
  82.     *  "sub", an array of nested blocks.
  83.     *
  84.     * @param array $arLines Template code lines
  85.     *
  86.     * @return array Array of blocks
  87.     *
  88.     * @static
  89.     */
  90.     function getBlocks($arLines)
  91.     {
  92.         $arBlocks = array();
  93.         $arRefs   = array();
  94.         $strRegex '/<!--\s+(BEGIN|END)\s+([a-zA-Z0-9_]*)\s+-->/';
  95.         foreach ($arLines as $nLine => $strLine{
  96.             if (!preg_match($strRegex$strLine$arMatches)) {
  97.                 continue;
  98.             }
  99.             $strType      $arMatches[1];
  100.             $strBlockName $arMatches[2];
  101.             if ($strType == 'BEGIN'{
  102.                 $arBlock = array(
  103.                     'name' => $strBlockName,
  104.                     'sub'  => array()
  105.                 );
  106.                 if (count($arRefs== 0{
  107.                     $arBlocks[$arBlock['name']] $arBlock;
  108.                     $arRefs[$strBlockName&$arBlocks[$arBlock['name']];
  109.                 else {
  110.                     end($arRefs);
  111.                     $strOldBlock key($arRefs);
  112.                     $arRefs[$strOldBlock]['sub'][$strBlockName$arBlock;
  113.                     $arRefs[$strBlockName=$arRefs[$strOldBlock]['sub'][$strBlockName];
  114.                 }
  115.             else {
  116.                 unset($arRefs[$strBlockName]);
  117.             }
  118.         }
  119.  
  120.         return $arBlocks;
  121.     }//function getBlocks($arLines)
  122.  
  123.  
  124.  
  125.     /**
  126.     * Creates a name that can be used as handle for a template,
  127.     *  from the given file name.
  128.     *
  129.     * @param string $strFile File name
  130.     *
  131.     * @return string Template name
  132.     */
  133.     function getTemplateNameFromFilename($strFile)
  134.     {
  135.         $strTplName basename($strFile);
  136.         //remove extension
  137.         $nDotPos strpos($strTplName'.');
  138.         if ($nDotPos !== false{
  139.             $strTplName substr($strTplName0$nDotPos);
  140.         }
  141.  
  142.         return $strTplName;
  143.     }//function getTemplateNameFromFilename($strFile)
  144.  
  145. }//class HTML_Template_PHPLIB_Generator
  146.  
  147. ?>

Documentation generated on Mon, 11 Mar 2019 15:10:14 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.