Source for file THtmlRenderer.php
Documentation is available at THtmlRenderer.php
namespace
Adianti \
Widget \
Template ;
use
ApplicationTranslator ;
* @author Pablo Dall'Oglio
* @copyright Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
* @license http://www.adianti.com.br/framework-license
private $enabledSections ;
private $enabledTranslation ;
private $HTMLOutputConversion ;
* @param $path HTML resource path
$this -> enabledSections =
array ( ) ;
$this -> enabledTranslation =
FALSE ;
$this -> HTMLOutputConversion =
true ;
public static function create ( $path , $replaces )
$html -> enableSection ( 'main' , $replaces ) ;
* Disable htmlspecialchars on output
$this -> HTMLOutputConversion =
false ;
* Enable translation inside template
$this -> enabledTranslation =
TRUE ;
* Enable a HTML section to show
* @param $sectionName Section name
* @param $replacements Array of replacements for this section
* @param $repeat Define if the section is repeatable
public function enableSection ( $sectionName , $replacements =
NULL , $repeat =
FALSE )
$this -> enabledSections [ ] =
$sectionName ;
$this -> replacements [ $sectionName ] =
$replacements ;
$this -> repeatSection [ $sectionName ] =
$repeat ;
$this -> enabledSections =
array_diff ( $this -> enabledSections , [ $sectionName ] ) ;
unset
( $this -> replacements [ $sectionName ] ) ;
unset
( $this -> repeatSection [ $sectionName ] ) ;
* Replace the content with array of replacements
* @param $replacements array of replacements
* @param $content content to be replaced
private function replace ( & $replacements , $content )
foreach ( $replacements as $variable =>
$value )
$value_original =
$value ;
if ( substr ( $value , 0 , 4 ) ==
'RAW:' )
else if ( $this -> HTMLOutputConversion )
$value =
htmlspecialchars ( $value , ENT_QUOTES |
ENT_HTML5 , 'UTF-8' ) ; // TAG value
$content =
str_replace ( '{$' .
$variable .
'}' , $value , $content ) ;
$content =
str_replace ( '{{' .
$variable .
'}}' , $value , $content ) ;
$content =
str_replace ( '{$' .
$variable .
'|raw}' , $value_original , $content ) ;
$content =
str_replace ( '{{' .
$variable .
'|raw}}' , $value_original , $content ) ;
$content =
str_replace ( '{$' .
$variable .
'}' , $output , $content ) ;
$content =
str_replace ( '{{' .
$variable .
'}}' , $output , $content ) ;
$replacements [ $variable ] =
'RAW:' .
$output ;
$vars =
$value -> getAttributes ( ) ;
$vars [ ] =
$value -> getPrimaryKey ( ) ;
else if ( ! $value instanceof
self )
foreach ( $vars as $propname )
$replace =
$value -> $propname ;
if ( $this -> HTMLOutputConversion )
$replace =
htmlspecialchars ( $replace , ENT_QUOTES |
ENT_HTML5 , 'UTF-8' ) ; // TAG value
$content =
str_replace ( '{$' .
$variable .
'->' .
$propname .
'}' , $replace , $content ) ;
$content =
str_replace ( '{{' .
$variable .
'->' .
$propname .
'}}' , $replace , $content ) ;
$content =
str_replace ( '{$' .
$variable .
'}' , '' , $content ) ;
$content =
str_replace ( '{{' .
$variable .
'}}' , '' , $content ) ;
else if ( is_array ( $value )) // embedded repeated section
// there is a template for this variable
if ( isset
( $this -> buffer [ $variable ] ))
$tpl =
$this -> buffer [ $variable ] ;
foreach ( $value as $replace )
$agg .=
$this -> replace ( $replace , $tpl ) ;
$content =
str_replace ( '{{' .
$variable .
'}}' , $agg , $content ) ;
// replace some php functions
* Show the HTML and the enabled sections
$opened_sections =
array ( ) ;
$sections_stack =
array ( 'main' ) ;
$array_content =
array ( ) ;
$content =
$this -> template ;
if ( $this -> enabledTranslation )
$content =
ApplicationTranslator :: translateTemplate ( $content ) ;
$array_content =
preg_split ( '/\n|\r\n?/' , $content ) ;
foreach ( $array_content as $line )
$line_clear =
trim ( $line ) ;
if ( ( substr ( $line_clear , 0 , 5 ) ==
'<!--[' ) AND ( substr ( $line_clear , -
4 ) ==
']-->' ) AND ( substr ( $line_clear , 0 , 6 ) !==
'<!--[/' ) )
$previousSection =
$sectionName ;
$sectionName =
substr ( $line_clear , 5 , strpos ( $line_clear , ']-->' ) -
5 ) ;
$sections_stack [ ] =
$sectionName ;
$this -> buffer [ $sectionName ] =
'' ;
$opened_sections [ $sectionName ] =
TRUE ;
$found =
self :: recursiveKeyArraySearch ( $previousSection , $this -> replacements ) ;
// turns section repeatable if it occurs inside parent section
if ( isset
( $this -> replacements [ $previousSection ] [ $sectionName ] ) OR
isset
( $this -> replacements [ $previousSection ] [ 0 ] [ $sectionName ] ) OR
isset
( $found [ $sectionName ] ) OR
isset
( $found [ 0 ] [ $sectionName ] ) )
$this -> repeatSection [ $sectionName ] =
TRUE ;
// section inherits replacements from parent session
if ( isset
( $this -> replacements [ $previousSection ] [ $sectionName ] ) &&
is_array ( $this -> replacements [ $previousSection ] [ $sectionName ] ))
$this -> replacements [ $sectionName ] =
$this -> replacements [ $previousSection ] [ $sectionName ] ;
else if ( ( substr ( $line_clear , 0 , 6 ) ==
'<!--[/' ) )
$sectionName =
substr ( $line_clear , 6 , strpos ( $line_clear , ']-->' ) -
6 ) ;
$opened_sections [ $sectionName ] =
FALSE ;
$previousSection =
end ( $sections_stack ) ;
// embbed current section as a variable inside the parent section
if ( isset
( $this -> repeatSection [ $previousSection ] ) AND $this -> repeatSection [ $previousSection ] )
$this -> buffer [ $previousSection ] .=
'{{' .
$sectionName .
'}}' ;
// if the section is repeatable and the parent is not (else), process replaces recursively
if (( isset
( $this -> repeatSection [ $sectionName ] ) AND $this -> repeatSection [ $sectionName ] ))
// if the section is repeatable, repeat the content according to its replacements
if ( isset
( $this -> replacements [ $sectionName ] ))
foreach ( $this -> replacements [ $sectionName ] as $iteration_replacement )
$processed .=
$this -> replace ( $iteration_replacement , $this -> buffer [ $sectionName ] ) ;
$sectionName =
end ( $sections_stack ) ;
else if ( in_array ( $sectionName , $this -> enabledSections )) // if the section is enabled
if ( ! $this -> repeatSection [ $sectionName ] ) // not repeatable, just echo
// print the line with the replacements
if ( isset
( $this -> replacements [ $sectionName ] ))
print
$this -> replace ( $this -> replacements [ $sectionName ] , $line .
"\n" ) ;
if ( ! isset
( $sectionName ))
if ( empty ( $this -> buffer [ $sectionName ] ))
$this -> buffer [ $sectionName ] =
'' ;
$this -> buffer [ $sectionName ] .=
$line .
"\n" ;
// check for unclosed sections
foreach ( $opened_sections as $section =>
$opened )
* Static search in memory structure
foreach ( $haystack as $key =>
$value )
else if ( is_array ( $value ) &&
self :: recursiveKeyArraySearch ( $needle , $value ) !==
false )
return self :: recursiveKeyArraySearch ( $needle , $value ) ;
* Returns the HTML content as a string