21 require_once
'PEAR.php';
24 define(
'IT_ERROR', -1);
25 define(
'IT_TPL_NOT_FOUND', -2);
26 define(
'IT_BLOCK_NOT_FOUND', -3);
27 define(
'IT_BLOCK_DUPLICATE', -4);
28 define(
'IT_UNKNOWN_OPTION', -6);
355 'preserve_data' =>
false,
375 $this->variablesRegExp =
'@' . $this->openingDelimiter .
376 '(' . $this->variablenameRegExp .
')' .
377 $this->closingDelimiter .
'@sm';
378 $this->removeVariablesRegExp =
'@' . $this->openingDelimiter .
379 "\s*(" . $this->variablenameRegExp .
380 ")\s*" . $this->closingDelimiter .
'@sm';
382 $this->blockRegExp =
'@<!--\s+BEGIN\s+(' . $this->blocknameRegExp .
383 ')\s+-->(.*)<!--\s+END\s+\1\s+-->@sm';
399 if (array_key_exists($option, $this->_options)) {
400 $this->_options[$option] = $value;
425 foreach (
$options as $option => $value) {
426 $error = $this->
setOption($option, $value);
440 function show($block =
'__global__')
442 print $this->
get($block);
454 function get($block =
'__global__')
456 if ($block ==
'__global__' && !$this->flagGlobalParsed) {
457 $this->
parse(
'__global__');
460 if (!isset($this->blocklist[$block])) {
469 if (isset($this->blockdata[$block])) {
470 $ret = $this->blockdata[$block];
471 if ($this->clearCache) {
472 unset($this->blockdata[$block]);
474 if ($this->_options[
'preserve_data']) {
476 $this->openingDelimiter .
477 '%preserved%' . $this->closingDelimiter,
478 $this->openingDelimiter,
496 function parse($block =
'__global__', $flag_recursion =
false)
498 static $regs, $values;
500 if (!isset($this->blocklist[$block])) {
507 if ($block ==
'__global__') {
508 $this->flagGlobalParsed =
true;
511 if (!$flag_recursion) {
515 $outer = $this->blocklist[$block];
518 if ($this->clearCacheOnParse) {
519 foreach ($this->variableCache as $name => $value) {
520 $regs[] = $this->openingDelimiter .
525 $this->variableCache = array();
527 foreach ($this->blockvariables[$block] as $allowedvar => $v) {
529 if (isset($this->variableCache[$allowedvar])) {
530 $regs[] = $this->openingDelimiter .
532 $values[] = $this->variableCache[$allowedvar];
533 unset($this->variableCache[$allowedvar]);
539 if (isset($this->blockinner[$block])) {
540 foreach ($this->blockinner[$block] as $k => $innerblock) {
542 $this->
parse($innerblock,
true);
543 if ($this->blockdata[$innerblock] !=
'') {
547 $placeholder = $this->openingDelimiter .
"__" .
549 $outer = str_replace(
551 $this->blockdata[$innerblock], $outer
553 $this->blockdata[$innerblock] =
"";
558 if (!$flag_recursion && 0 != count($values)) {
559 if ($this->_options[
'use_preg']) {
560 $regs = array_map(array(
561 &$this,
'_addPregDelimiters'),
564 $funcReplace =
'preg_replace';
566 $funcReplace =
'str_replace';
569 if ($this->_options[
'preserve_data']) {
571 array(&$this,
'_preserveOpeningDelimiter'), $values
575 $outer = $funcReplace($regs, $values, $outer);
577 if ($this->removeUnknownVariables) {
578 $outer = preg_replace($this->removeVariablesRegExp,
"", $outer);
583 if (!$this->removeEmptyBlocks) {
584 $this->blockdata[$block ].= $outer;
586 if (isset($this->touchedBlocks[$block])) {
587 $this->blockdata[$block] .= $outer;
588 unset($this->touchedBlocks[$block]);
592 if (empty($this->blockdata[$block])) {
593 $this->blockdata[$block] = $outer;
595 $this->blockdata[$block] .= $outer;
609 return $this->
parse($this->currentBlock);
628 if (is_array($variable)) {
629 $this->variableCache = array_merge(
630 $this->variableCache, $variable
633 $this->variableCache[$variable] = $value;
649 if (!isset($this->blocklist[$block])) {
656 $this->currentBlock = $block;
672 if (!isset($this->blocklist[$block])) {
678 $this->touchedBlocks[$block] =
true;
696 require_once(
'./Services/GlobalCache/classes/class.ilGlobalCache.php');
699 if ($blockdata = $blocks->get($this->real_filename)) {
700 $this->blockdata = $blockdata[
'blockdata'];
701 $this->blocklist = $blockdata[
'blocklist'];
707 $blocks->set($this->real_filename, $blockdata, 60);
711 $this->
template =
'';
714 if ($blockvariables = $variables->get($this->real_filename)) {
718 $variables->set($this->real_filename, $this->blockvariables, 60);
732 $this->err = array();
734 $this->currentBlock =
'__global__';
736 $this->variableCache = array();
737 $this->blocklist = array();
738 $this->touchedBlocks = array();
740 $this->flagBlocktrouble =
false;
741 $this->flagGlobalParsed =
false;
758 $removeEmptyBlocks =
true)
763 if ($template ==
'' && $this->flagCacheTemplatefile) {
764 $this->variableCache = array();
765 $this->blockdata = array();
766 $this->touchedBlocks = array();
767 $this->currentBlock =
'__global__';
769 $this->
template =
'<!-- BEGIN __global__ -->' . $template .
770 '<!-- END __global__ -->';
774 if ($this->flagBlocktrouble) {
793 $removeUnknownVariables =
true,
794 $removeEmptyBlocks =
true )
797 if (!$this->flagCacheTemplatefile ||
804 return $template !=
'' ?
806 $template,$removeUnknownVariables, $removeEmptyBlocks
823 if ($root !=
'' && substr($root, -1) !=
'/') {
827 $this->fileRoot = $root;
835 foreach ($this->blocklist as $name => $content) {
836 preg_match_all($this->variablesRegExp, $content, $regs);
838 if (count($regs[1]) != 0) {
839 foreach ($regs[1] as $k => $var) {
840 $this->blockvariables[$name][$var] =
true;
843 $this->blockvariables[$name] = array();
856 foreach ($this->blockvariables[
'__global__'] as $allowedvar => $v) {
857 if (isset($this->variableCache[$allowedvar])) {
858 $regs[] =
'@' . $this->openingDelimiter .
859 $allowedvar . $this->closingDelimiter .
'@';
860 $values[] = $this->variableCache[$allowedvar];
861 unset($this->variableCache[$allowedvar]);
865 return array($regs, $values);
876 $blocklist = array();
877 if (preg_match_all($this->blockRegExp, $string, $regs, PREG_SET_ORDER)) {
878 foreach ($regs as $k => $match) {
879 $blockname = $match[1];
880 $blockcontent = $match[2];
882 if (isset($this->blocklist[$blockname])) {
888 $this->flagBlocktrouble =
true;
891 $this->blocklist[$blockname] = $blockcontent;
892 $this->blockdata[$blockname] =
"";
894 $blocklist[] = $blockname;
897 foreach ($inner as $k => $name) {
899 '@<!--\s+BEGIN\s+%s\s+-->(.*)<!--\s+END\s+%s\s+-->@sm',
904 $this->blocklist[$blockname] = preg_replace(
906 $this->openingDelimiter .
907 '__' . $name .
'__' .
908 $this->closingDelimiter,
909 $this->blocklist[$blockname]
911 $this->blockinner[$blockname][] = $name;
912 $this->blockparents[$name] = $blockname;
927 if (
$filename{0} ==
'/' && substr($this->fileRoot, -1) ==
'/') {
933 require_once(
'./Services/GlobalCache/classes/class.ilGlobalCache.php');
936 if(!$content = $ilGlobalCache->get(
$filename)) {
952 $content = fread($fh, $fsize);
953 $ilGlobalCache->set(
$filename, $content, 60);
958 return preg_replace_callback(
959 "#<!-- INCLUDE (.*) -->#im",
961 return $this->
getFile($hit[1]);
976 return '@' . $str .
'@';
987 return (
false === strpos($str, $this->openingDelimiter))?
990 $this->openingDelimiter,
991 $this->openingDelimiter .
992 '%preserved%' . $this->closingDelimiter,
1007 static $errorMessages;
1008 if (!isset($errorMessages)) {
1009 $errorMessages = array(
1015 ' uniquewithin a template.'.
1016 ' Found "' . $blockname .
'" twice.'.
1017 'Unpredictable results '.
1024 $value = $value->getCode();
1027 return isset($errorMessages[$value]) ?
1028 $errorMessages[$value] : $errorMessages[
IT_ERROR];
loadTemplatefile( $filename, $removeUnknownVariables=true, $removeEmptyBlocks=true)
Reads a template file from the disk.
touchBlock($block)
Preserves an empty block even if removeEmptyBlocks is true.
show($block='__global__')
Print a certain block with all replacements done.
_addPregDelimiters($str)
Adds delimiters to a string, so it can be used as a pattern in preg_* functions.
$removeVariablesRegExp
RegExp used to strip unused variable placeholder.
getGlobalvariables()
Returns a list of all global variables.
static getInstance($component)
errorMessage($value, $blockname='')
Return a textual error message for a IT error code.
setCurrentBlock($block='__global__')
Sets the name of the current block that is the block where variables are added.
setOptions($options)
Sets the options for the template class.
HTML_Template_IT($root='', $options=null)
Builds some complex regular expressions and optinally sets the file root directory.
free()
Clears all datafields of the object.
setVariable($variable, $value='')
Sets a variable value.
if(!is_array($argv)) $options
setRoot($root)
Sets the file root.
$_options
$_options['preserve_data'] Whether to substitute variables and remove empty placeholders in data pass...
parse($block='__global__', $flag_recursion=false)
Parses the given block.
parseCurrentBlock()
Parses the current block.
buildBlockvariablelist()
Build a list of all variables within of a block.
static log($message, $log_level)
$_hiddenBlocks
List of blocks which should not be shown even if not "empty".
setTemplate( $template, $removeUnknownVariables=true, $removeEmptyBlocks=true)
Sets the template.
$touchedBlocks
List of blocks to preverse even if they are "empty".
& raiseError($message=null, $code=null, $mode=null, $options=null, $userinfo=null, $error_class=null, $skipmsg=false)
This method is a wrapper that returns an instance of the configured error class with this object's de...
_preserveOpeningDelimiter($str)
Replaces an opening delimiter by a special string.
init()
Clears all datafields of the object and rebuild the internal blocklist.
setOption($option, $value)
Sets the option for the template class.
findBlocks($string)
Recusively builds a list of all blocks within the template.
$lastTemplatefile
EXPERIMENTAL! FIXME!
getFile($filename)
Reads a file from disk and returns its content.
Integrated Template - IT.
isError($data, $code=null)
Tell whether a value is a PEAR error.