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);
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;
421 foreach (
$options as $option => $value) {
422 $error = $this->
setOption($option, $value);
433 public function show($block =
'__global__')
435 print $this->
get($block);
447 public function get($block =
'__global__')
449 if ($block ==
'__global__' && !$this->flagGlobalParsed) {
450 $this->
parse(
'__global__');
453 if (!isset($this->blocklist[$block])) {
455 '"' . $block .
"'"));
458 if (isset($this->blockdata[$block])) {
459 $ret = $this->blockdata[$block];
460 if ($this->clearCache) {
461 unset($this->blockdata[$block]);
463 if ($this->_options[
'preserve_data']) {
465 $this->openingDelimiter .
466 '%preserved%' . $this->closingDelimiter,
467 $this->openingDelimiter,
485 public function parse($block =
'__global__', $flag_recursion =
false)
489 if (!isset($this->blocklist[$block])) {
491 '"' . $block .
"'"));
494 if ($block ==
'__global__') {
495 $this->flagGlobalParsed =
true;
498 if (!$flag_recursion) {
502 $outer = $this->blocklist[$block];
505 if ($this->clearCacheOnParse) {
506 foreach ($this->variableCache as
$name => $value) {
507 $regs[] = $this->openingDelimiter .
512 $this->variableCache = array();
514 foreach ($this->blockvariables[$block] as $allowedvar => $v) {
515 if (isset($this->variableCache[$allowedvar])) {
516 $regs[] = $this->openingDelimiter .
518 $values[] = $this->variableCache[$allowedvar];
519 unset($this->variableCache[$allowedvar]);
525 if (isset($this->blockinner[$block])) {
526 foreach ($this->blockinner[$block] as $k => $innerblock) {
527 $this->
parse($innerblock,
true);
528 if ($this->blockdata[$innerblock] !=
'') {
532 $placeholder = $this->openingDelimiter .
"__" .
534 $outer = str_replace(
536 $this->blockdata[$innerblock],
539 $this->blockdata[$innerblock] =
"";
543 if (!$flag_recursion && 0 != count($values)) {
544 if ($this->_options[
'use_preg']) {
547 &$this,
'_addPregDelimiters'),
550 $funcReplace =
'preg_replace';
552 $funcReplace =
'str_replace';
555 if ($this->_options[
'preserve_data']) {
557 array(&$this,
'_preserveOpeningDelimiter'),
562 $outer = $funcReplace($regs, $values, $outer);
564 if ($this->removeUnknownVariables) {
565 $outer = preg_replace($this->removeVariablesRegExp,
"", $outer);
570 if (!$this->removeEmptyBlocks) {
571 $this->blockdata[$block ] .= $outer;
573 if (isset($this->touchedBlocks[$block])) {
574 $this->blockdata[$block] .= $outer;
575 unset($this->touchedBlocks[$block]);
579 if (empty($this->blockdata[$block])) {
580 $this->blockdata[$block] = $outer;
582 $this->blockdata[$block] .= $outer;
596 return $this->
parse($this->currentBlock);
615 if (is_array($variable)) {
616 $this->variableCache = array_merge(
617 $this->variableCache,
621 $this->variableCache[$variable] = $value;
636 if (!isset($this->blocklist[$block])) {
638 '"' . $block .
"'"));
641 $this->currentBlock = $block;
657 if (!isset($this->blocklist[$block])) {
659 '"' . $block .
"'"));
662 $this->touchedBlocks[$block] =
true;
680 require_once(
'./Services/GlobalCache/classes/class.ilGlobalCache.php');
683 if (
$blockdata = $blocks->get($this->real_filename)) {
691 $blocks->set($this->real_filename,
$blockdata, 60);
695 $this->
template =
'';
702 $variables->set($this->real_filename, $this->blockvariables, 60);
716 $this->err = array();
718 $this->currentBlock =
'__global__';
720 $this->variableCache = array();
721 $this->blocklist = array();
722 $this->touchedBlocks = array();
724 $this->flagBlocktrouble =
false;
725 $this->flagGlobalParsed =
false;
749 if (
$template ==
'' && $this->flagCacheTemplatefile) {
750 $this->variableCache = array();
751 $this->blockdata = array();
752 $this->touchedBlocks = array();
753 $this->currentBlock =
'__global__';
755 $this->
template =
'<!-- BEGIN __global__ -->' .
$template .
756 '<!-- END __global__ -->';
760 if ($this->flagBlocktrouble) {
784 if (!$this->flagCacheTemplatefile ||
812 if (
$root !=
'' && substr(
$root, -1) !=
'/') {
816 $this->fileRoot =
$root;
824 foreach ($this->blocklist as
$name => $content) {
825 preg_match_all($this->variablesRegExp, $content, $regs);
827 if (count($regs[1]) != 0) {
828 foreach ($regs[1] as $k => $var) {
829 $this->blockvariables[
$name][$var] =
true;
832 $this->blockvariables[
$name] = array();
845 foreach ($this->blockvariables[
'__global__'] as $allowedvar => $v) {
846 if (isset($this->variableCache[$allowedvar])) {
847 $regs[] =
'@' . $this->openingDelimiter .
848 $allowedvar . $this->closingDelimiter .
'@';
849 $values[] = $this->variableCache[$allowedvar];
850 unset($this->variableCache[$allowedvar]);
866 if (preg_match_all($this->blockRegExp, $string, $regs, PREG_SET_ORDER)) {
867 foreach ($regs as $k => $match) {
868 $blockname = $match[1];
869 $blockcontent = $match[2];
871 if (isset($this->blocklist[$blockname])) {
878 $this->blocklist[$blockname] = $blockcontent;
879 $this->blockdata[$blockname] =
"";
884 foreach ($inner as $k =>
$name) {
886 '@<!--\s+BEGIN\s+%s\s+-->(.*)<!--\s+END\s+%s\s+-->@sm',
891 $this->blocklist[$blockname] = preg_replace(
893 $this->openingDelimiter .
894 '__' .
$name .
'__' .
895 $this->closingDelimiter,
896 $this->blocklist[$blockname]
898 $this->blockinner[$blockname][] =
$name;
899 $this->blockparents[
$name] = $blockname;
914 if (
$filename[0] ==
'/' && substr($this->fileRoot, -1) ==
'/') {
920 require_once(
'./Services/GlobalCache/classes/class.ilGlobalCache.php');
923 if (!$content = $ilGlobalCache->get(
$filename)) {
935 $content = fread($fh, $fsize);
936 $ilGlobalCache->set(
$filename, $content, 60);
962 return '@' . $str .
'@';
973 return (
false === strpos($str, $this->openingDelimiter))?
976 $this->openingDelimiter,
977 $this->openingDelimiter .
978 '%preserved%' . $this->closingDelimiter,
993 static $errorMessages;
994 if (!isset($errorMessages)) {
995 $errorMessages = array(
1001 ' uniquewithin a template.' .
1002 ' Found "' . $blockname .
'" twice.' .
1003 'Unpredictable results ' .
1009 return isset($errorMessages[$value]) ?
1010 $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.