21 include_once(
"./Services/UICore/lib/html-it/exceptions/class.ilTemplateException.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);
357 'preserve_data' =>
false,
374 if (!is_null($options)) {
377 $this->variablesRegExp =
'@' . $this->openingDelimiter .
378 '(' . $this->variablenameRegExp .
')' .
379 $this->closingDelimiter .
'@sm';
380 $this->removeVariablesRegExp =
'@' . $this->openingDelimiter .
381 "\s*(" . $this->variablenameRegExp .
382 ")\s*" . $this->closingDelimiter .
'@sm';
384 $this->blockRegExp =
'@<!--\s+BEGIN\s+(' . $this->blocknameRegExp .
385 ')\s+-->(.*)<!--\s+END\s+\1\s+-->@sm';
401 if (array_key_exists($option, $this->_options)) {
402 $this->_options[$option] = $value;
422 if (is_array($options)) {
423 foreach ($options as $option => $value) {
424 $error = $this->
setOption($option, $value);
435 public function show($block =
'__global__')
437 print $this->
get($block);
449 public function get($block =
'__global__')
451 if ($block ==
'__global__' && !$this->flagGlobalParsed) {
452 $this->
parse(
'__global__');
455 if (!isset($this->blocklist[$block])) {
457 '"' . $block .
"'"));
460 if (isset($this->blockdata[$block])) {
461 $ret = $this->blockdata[$block];
462 if ($this->clearCache) {
463 unset($this->blockdata[$block]);
465 if ($this->_options[
'preserve_data']) {
467 $this->openingDelimiter .
468 '%preserved%' . $this->closingDelimiter,
469 $this->openingDelimiter,
487 public function parse($block =
'__global__', $flag_recursion =
false)
489 static $regs, $values;
491 if (!isset($this->blocklist[$block])) {
493 '"' . $block .
"'"));
496 if ($block ==
'__global__') {
497 $this->flagGlobalParsed =
true;
500 if (!$flag_recursion) {
504 $outer = $this->blocklist[$block];
507 if ($this->clearCacheOnParse) {
508 foreach ($this->variableCache as
$name => $value) {
509 $regs[] = $this->openingDelimiter .
514 $this->variableCache = array();
516 foreach ($this->blockvariables[$block] as $allowedvar => $v) {
517 if (isset($this->variableCache[$allowedvar])) {
518 $regs[] = $this->openingDelimiter .
520 $values[] = $this->variableCache[$allowedvar];
521 unset($this->variableCache[$allowedvar]);
527 if (isset($this->blockinner[$block])) {
528 foreach ($this->blockinner[$block] as $k => $innerblock) {
529 $this->
parse($innerblock,
true);
530 if ($this->blockdata[$innerblock] !=
'') {
534 $placeholder = $this->openingDelimiter .
"__" .
536 $outer = str_replace(
538 $this->blockdata[$innerblock],
541 $this->blockdata[$innerblock] =
"";
545 if (!$flag_recursion && 0 != count($values)) {
546 if ($this->_options[
'use_preg']) {
549 &$this,
'_addPregDelimiters'),
552 $funcReplace =
'preg_replace';
554 $funcReplace =
'str_replace';
557 if ($this->_options[
'preserve_data']) {
559 array(&$this,
'_preserveOpeningDelimiter'),
564 $outer = $funcReplace($regs, $values, $outer);
566 if ($this->removeUnknownVariables) {
567 $outer = preg_replace($this->removeVariablesRegExp,
"", $outer);
572 if (!$this->removeEmptyBlocks) {
573 $this->blockdata[$block ] .= $outer;
575 if (isset($this->touchedBlocks[$block])) {
576 $this->blockdata[$block] .= $outer;
577 unset($this->touchedBlocks[$block]);
581 if (empty($this->blockdata[$block])) {
582 $this->blockdata[$block] = $outer;
584 $this->blockdata[$block] .= $outer;
598 return $this->
parse($this->currentBlock);
617 if (is_array($variable)) {
618 $this->variableCache = array_merge(
619 $this->variableCache,
623 $this->variableCache[$variable] = $value;
638 if (!isset($this->blocklist[$block])) {
640 '"' . $block .
"'"));
643 $this->currentBlock = $block;
659 if (!isset($this->blocklist[$block])) {
661 '"' . $block .
"'"));
664 $this->touchedBlocks[$block] =
true;
682 require_once(
'./Services/GlobalCache/classes/class.ilGlobalCache.php');
685 if (
$blockdata = $blocks->get($this->real_filename)) {
693 $blocks->set($this->real_filename,
$blockdata, 60);
697 $this->
template =
'';
704 $variables->set($this->real_filename, $this->blockvariables, 60);
718 $this->err = array();
720 $this->currentBlock =
'__global__';
722 $this->variableCache = array();
723 $this->blocklist = array();
724 $this->touchedBlocks = array();
726 $this->flagBlocktrouble =
false;
727 $this->flagGlobalParsed =
false;
751 if (
$template ==
'' && $this->flagCacheTemplatefile) {
752 $this->variableCache = array();
753 $this->blockdata = array();
754 $this->touchedBlocks = array();
755 $this->currentBlock =
'__global__';
757 $this->
template =
'<!-- BEGIN __global__ -->' .
$template .
758 '<!-- END __global__ -->';
762 if ($this->flagBlocktrouble) {
786 if (!$this->flagCacheTemplatefile ||
814 if ($root !=
'' && substr($root, -1) !=
'/') {
818 $this->fileRoot = $root;
826 foreach ($this->blocklist as
$name => $content) {
827 preg_match_all($this->variablesRegExp, $content, $regs);
829 if (count($regs[1]) != 0) {
830 foreach ($regs[1] as $k => $var) {
831 $this->blockvariables[
$name][$var] =
true;
834 $this->blockvariables[
$name] = array();
847 foreach ($this->blockvariables[
'__global__'] as $allowedvar => $v) {
848 if (isset($this->variableCache[$allowedvar])) {
849 $regs[] =
'@' . $this->openingDelimiter .
850 $allowedvar . $this->closingDelimiter .
'@';
851 $values[] = $this->variableCache[$allowedvar];
852 unset($this->variableCache[$allowedvar]);
856 return array($regs, $values);
868 if (preg_match_all($this->blockRegExp, $string, $regs, PREG_SET_ORDER)) {
869 foreach ($regs as $k => $match) {
870 $blockname = $match[1];
871 $blockcontent = $match[2];
873 if (isset($this->blocklist[$blockname])) {
880 $this->blocklist[$blockname] = $blockcontent;
881 $this->blockdata[$blockname] =
"";
886 foreach ($inner as $k =>
$name) {
888 '@<!--\s+BEGIN\s+%s\s+-->(.*)<!--\s+END\s+%s\s+-->@sm',
893 $this->blocklist[$blockname] = preg_replace(
895 $this->openingDelimiter .
896 '__' .
$name .
'__' .
897 $this->closingDelimiter,
898 $this->blocklist[$blockname]
900 $this->blockinner[$blockname][] =
$name;
901 $this->blockparents[
$name] = $blockname;
916 if (
$filename[0] ==
'/' && substr($this->fileRoot, -1) ==
'/') {
922 require_once(
'./Services/GlobalCache/classes/class.ilGlobalCache.php');
925 if (!$content = $ilGlobalCache->get(
$filename)) {
937 $content = fread($fh, $fsize);
938 $ilGlobalCache->set(
$filename, $content, 60);
964 return '@' . $str .
'@';
975 return (
false === strpos($str, $this->openingDelimiter))?
978 $this->openingDelimiter,
979 $this->openingDelimiter .
980 '%preserved%' . $this->closingDelimiter,
995 static $errorMessages;
996 if (!isset($errorMessages)) {
997 $errorMessages = array(
1003 ' uniquewithin a template.' .
1004 ' Found "' . $blockname .
'" twice.' .
1005 'Unpredictable results ' .
1011 return isset($errorMessages[$value]) ?
1012 $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.
free()
Clears all datafields of the object.
setVariable($variable, $value='')
Sets a variable value.
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".
__construct($root='', $options=null)
Builds some complex regular expressions and optinally sets the file root directory.
setTemplate( $template, $removeUnknownVariables=true, $removeEmptyBlocks=true)
Sets the template.
$touchedBlocks
List of blocks to preverse even if they are "empty".
_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.