Lançado Adianti Framework 7.6!
Clique aqui para saber mais
menu

Adianti Solutions

API

Adianti, Framework, PHP, MVC, Active record, Front controller, IDE, RAD, Web, multiplataforma, geração de código, desenvolvimento rápido, relatórios, formulários, listagens, datagrids, gráficos, banco de dados, padrões de projeto, design patterns API do Adianti Framework.
API Docs
code
Selecione a classe

Source for file THtmlRenderer.php

Documentation is available at THtmlRenderer.php

  1. <?php
  2. namespace Adianti\Widget\Template;
  3.  
  4. use Adianti\Core\AdiantiCoreTranslator;
  5. use Adianti\Util\AdiantiTemplateHandler;
  6.  
  7. use Exception;
  8. use ApplicationTranslator;
  9.  
  10. /**
  11.  * Html Renderer
  12.  *
  13.  * @version    7.4
  14.  * @package    widget
  15.  * @subpackage template
  16.  * @author     Pablo Dall'Oglio
  17.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  18.  * @license    http://www.adianti.com.br/framework-license
  19.  */
  20. {
  21.     private $path;
  22.     private $buffer;
  23.     private $template;
  24.     private $sections;
  25.     private $replacements;
  26.     private $enabledSections;
  27.     private $repeatSection;
  28.     private $enabledTranslation;
  29.     private $HTMLOutputConversion;
  30.     
  31.     /**
  32.      * Constructor method
  33.      * 
  34.      * @param $path  HTML resource path
  35.      */
  36.     public function __construct($path)
  37.     {
  38.         if (!file_exists($path))
  39.         {
  40.             throw new Exception(AdiantiCoreTranslator::translate('File not found').': ' $path);
  41.         }
  42.         $this->enabledSections array();
  43.         $this->enabledTranslation FALSE;
  44.         $this->buffer array();
  45.         $this->HTMLOutputConversion true;
  46.         
  47.         if (file_exists($path))
  48.         {
  49.             $this->template file_get_contents($path);
  50.         }
  51.     }
  52.     
  53.     /**
  54.      * Creates HTML Renderer
  55.      */
  56.     public static function create($path$replaces)
  57.     {
  58.         $html new self($path);
  59.         $html->enableSection('main'$replaces);
  60.         return $html;
  61.     }
  62.     
  63.     /**
  64.      * Disable htmlspecialchars on output
  65.      */
  66.     public function disableHtmlConversion()
  67.     {
  68.         $this->HTMLOutputConversion false;
  69.     }
  70.     
  71.     /**
  72.      * Enable translation inside template
  73.      */
  74.     public function enableTranslation()
  75.     {
  76.         $this->enabledTranslation TRUE;
  77.     }
  78.     
  79.     /**
  80.      * Enable a HTML section to show
  81.      * 
  82.      * @param $sectionName Section name
  83.      * @param $replacements Array of replacements for this section
  84.      * @param $repeat Define if the section is repeatable
  85.      */
  86.     public function enableSection($sectionName$replacements NULL$repeat FALSE)
  87.     {
  88.         $this->enabledSections[$sectionName;
  89.         $this->replacements[$sectionName$replacements;
  90.         $this->repeatSection[$sectionName$repeat;
  91.     }
  92.     
  93.     /**
  94.      * Diable section
  95.      */
  96.     public function disableSection($sectionName)
  97.     {
  98.         $this->enabledSections array_diff($this->enabledSections[$sectionName]);
  99.         unset($this->replacements[$sectionName]);
  100.         unset($this->repeatSection[$sectionName]);
  101.     }
  102.     
  103.     /**
  104.      * Replace the content with array of replacements
  105.      * 
  106.      * @param $replacements array of replacements
  107.      * @param $content content to be replaced
  108.      */
  109.     private function replace(&$replacements$content)
  110.     {
  111.         if (is_array($replacements))
  112.         {
  113.             foreach ($replacements as $variable => $value)
  114.             {
  115.                 if (is_scalar($value))
  116.                 {
  117.                     $value_original $value;
  118.                     
  119.                     if (substr($value,0,4== 'RAW:')
  120.                     {
  121.                         $value substr($value,4);
  122.                     }
  123.                     else if ($this->HTMLOutputConversion)
  124.                     {
  125.                         $value htmlspecialchars($valueENT_QUOTES ENT_HTML5'UTF-8');   // TAG value
  126.                     }
  127.                     
  128.                     $content str_replace('{$'.$variable.'}',  $value$content);
  129.                     $content str_replace('{{'.$variable.'}}'$value$content);
  130.                     $content str_replace('{$'.$variable.'|raw}',  $value_original$content);
  131.                     $content str_replace('{{'.$variable.'|raw}}'$value_original$content);
  132.                 }
  133.                 else if (is_object($value))
  134.                 {
  135.                     if (method_exists($value'show'))
  136.                     {
  137.                         ob_start();
  138.                         $value->show();
  139.                         $output ob_get_contents();
  140.                         ob_end_clean();
  141.                         
  142.                         $content str_replace('{$'.$variable.'}',  $output$content);
  143.                         $content str_replace('{{'.$variable.'}}'$output$content);
  144.                         
  145.                         $replacements[$variable'RAW:' $output;
  146.                     }
  147.                     
  148.                     if (method_exists($value'getAttributes'))
  149.                     {
  150.                         $vars $value->getAttributes();
  151.                         $vars[$value->getPrimaryKey();
  152.                     }
  153.                     else if (!$value instanceof self)
  154.                     {
  155.                         $vars array_keys(get_object_vars($value));
  156.                     }
  157.                     
  158.                     if (isset($vars))
  159.                     {
  160.                         foreach ($vars as $propname)
  161.                         {
  162.                             if (is_scalar($variable.'->'.$propname))
  163.                             {
  164.                                 $replace $value->$propname;
  165.                                 if (is_scalar($replace))
  166.                                 {
  167.                                     if ($this->HTMLOutputConversion)
  168.                                     {
  169.                                         $replace htmlspecialchars($replaceENT_QUOTES ENT_HTML5'UTF-8');   // TAG value
  170.                                     }
  171.                                     
  172.                                     $content str_replace('{$'.$variable.'->'.$propname.'}',   $replace$content);
  173.                                     $content str_replace('{{'.$variable.'->'.$propname.'}}',  $replace$content);
  174.                                 }
  175.                             }
  176.                         }
  177.                     }
  178.                 }
  179.                 else if (is_null($value))
  180.                 {
  181.                     $content str_replace('{$'.$variable.'}',  ''$content);
  182.                     $content str_replace('{{'.$variable.'}}'''$content);
  183.                 }
  184.                 else if (is_array($value)) // embedded repeated section
  185.                 {
  186.                     // there is a template for this variable
  187.                     if (isset($this->buffer[$variable]))
  188.                     {
  189.                         $tpl $this->buffer[$variable];
  190.                         $agg '';
  191.                         foreach ($value as $replace)
  192.                         {
  193.                             $agg .= $this->replace($replace$tpl);
  194.                         }
  195.                         $content str_replace('{{'.$variable.'}}'$agg$content);
  196.                     }
  197.                 }
  198.             }
  199.         }
  200.         
  201.         // replace some php functions
  202.         $content AdiantiTemplateHandler::replaceFunctions($content);
  203.         
  204.         return $content;
  205.     }
  206.     
  207.     /**
  208.      * Show the HTML and the enabled sections
  209.      */
  210.     public function show()
  211.     {
  212.         $opened_sections array();
  213.         $sections_stack array('main');
  214.         $array_content array();
  215.         
  216.         if ($this->template)
  217.         {
  218.             $content $this->template;
  219.             if ($this->enabledTranslation)
  220.             {
  221.                 $content  ApplicationTranslator::translateTemplate($content);
  222.             }
  223.             
  224.             $array_content preg_split('/\n|\r\n?/'$content);
  225.             $sectionName null;
  226.             
  227.             // iterate line by line
  228.             foreach ($array_content as $line)
  229.             {
  230.                 $line_clear trim($line);
  231.                 $line_clear str_replace("\n"''$line_clear);
  232.                 $line_clear str_replace("\r"''$line_clear);
  233.                 $delimiter  FALSE;
  234.                 
  235.                 // detect section start
  236.                 if ( (substr($line_clear0,5)=='<!--['AND (substr($line_clear-4== ']-->'AND (substr($line_clear0,6)!=='<!--[/') )
  237.                 {
  238.                     $previousSection $sectionName;
  239.                     $sectionName substr($line_clear5strpos($line_clear']-->')-5);
  240.                     $sections_stack[$sectionName;
  241.                     $this->buffer[$sectionName'';
  242.                     $opened_sections[$sectionNameTRUE;
  243.                     $delimiter  TRUE;
  244.                     
  245.                     $found self::recursiveKeyArraySearch($previousSection$this->replacements);
  246.                     
  247.                     // turns section repeatable if it occurs inside parent section
  248.                     if (isset($this->replacements[$previousSection][$sectionName]OR
  249.                         isset($this->replacements[$previousSection][0][$sectionName]OR
  250.                         isset($found[$sectionName]OR
  251.                         isset($found[0][$sectionName]) )
  252.                     {
  253.                         $this->repeatSection[$sectionNameTRUE;
  254.                     }
  255.                     
  256.                     // section inherits replacements from parent session
  257.                     if (isset($this->replacements[$previousSection][$sectionName]&& is_array($this->replacements[$previousSection][$sectionName]))
  258.                     {
  259.                         $this->replacements[$sectionName$this->replacements[$previousSection][$sectionName];
  260.                     }
  261.                 }
  262.                 // detect section end
  263.                 else if ( (substr($line_clear0,6)=='<!--[/') )
  264.                 {
  265.                     $delimiter  TRUE;
  266.                     $sectionName substr($line_clear6strpos($line_clear']-->')-6);
  267.                     $opened_sections[$sectionNameFALSE;
  268.                     
  269.                     array_pop($sections_stack);
  270.                     $previousSection end($sections_stack);
  271.                     
  272.                     // embbed current section as a variable inside the parent section
  273.                     if (isset($this->repeatSection[$previousSection]AND $this->repeatSection[$previousSection])
  274.                     {
  275.                         $this->buffer[$previousSection.= '{{'.$sectionName.'}}';
  276.                     }
  277.                     else
  278.                     {
  279.                         // if the section is repeatable and the parent is not (else), process replaces recursively
  280.                         if ((isset($this->repeatSection[$sectionName]AND $this->repeatSection[$sectionName]))
  281.                         {
  282.                             $processed '';
  283.                             // if the section is repeatable, repeat the content according to its replacements
  284.                             if (isset($this->replacements[$sectionName]))
  285.                             {
  286.                                 foreach ($this->replacements[$sectionNameas $iteration_replacement)
  287.                                 {
  288.                                     $processed .= $this->replace($iteration_replacement$this->buffer[$sectionName]);
  289.                                 }
  290.                                 AdiantiTemplateHandler::processAttribution($processed$this->replacements);
  291.                                 print $processed;
  292.                                 $processed '';
  293.                             }
  294.                         }
  295.                     }
  296.                     
  297.                     $sectionName end($sections_stack);
  298.                 }
  299.                 else if (in_array($sectionName$this->enabledSections)) // if the section is enabled
  300.                 {
  301.                     if (!$this->repeatSection[$sectionName]// not repeatable, just echo
  302.                     {
  303.                         // print the line with the replacements
  304.                         if (isset($this->replacements[$sectionName]))
  305.                         {
  306.                             print $this->replace($this->replacements[$sectionName]$line "\n");
  307.                         }
  308.                         else
  309.                         {
  310.                             print $line "\n";
  311.                         }
  312.                     }
  313.  
  314.                 }
  315.                 
  316.                 if (!$delimiter)
  317.                 {
  318.                     if (!isset($sectionName))
  319.                     {
  320.                         $sectionName 'main';
  321.                         if (empty($this->buffer[$sectionName]))
  322.                         {
  323.                             $this->buffer[$sectionName'';
  324.                         }
  325.                     }
  326.                     
  327.                     $this->buffer[$sectionName.= $line "\n";
  328.                 }
  329.             }
  330.         }
  331.         
  332.         // check for unclosed sections
  333.         if ($opened_sections)
  334.         {
  335.             foreach ($opened_sections as $section => $opened)
  336.             {
  337.                 if ($opened)
  338.                 {
  339.                     throw new Exception(AdiantiCoreTranslator::translate('The section (^1) was not closed properly'$section));
  340.                 }
  341.             }
  342.         }
  343.     }
  344.     
  345.     /**
  346.      * Static search in memory structure
  347.      */
  348.     public static function recursiveKeyArraySearch($needle,$haystack)
  349.     {
  350.         if ($haystack)
  351.         {
  352.             foreach($haystack as $key=>$value)
  353.             {
  354.                 if($needle === $key)
  355.                 {
  356.                     return $value;
  357.                 }
  358.                 else if (is_array($value&& self::recursiveKeyArraySearch($needle,$value!== false)
  359.                 {
  360.                     return self::recursiveKeyArraySearch($needle,$value);
  361.                 }
  362.             }
  363.         }
  364.         return false;
  365.     }
  366.     
  367.     /**
  368.      * Returns the HTML content as a string
  369.      */
  370.     public function getContents()
  371.     {
  372.         ob_start();
  373.         $this->show();
  374.         $content ob_get_contents();
  375.         ob_end_clean();
  376.         return $content;
  377.     }
  378. }