19declare(strict_types=1);
41require_once __DIR__ .
'/../../exceptions/class.ilTemplateException.php';
304 'preserve_data' =>
false,
322 public function __construct(
string $root =
'', ?array $options =
null)
330 if (!is_null($options)) {
333 $this->variablesRegExp =
'@' . $this->openingDelimiter .
334 '(' . $this->variablenameRegExp .
')' .
335 $this->closingDelimiter .
'@sm';
336 $this->removeVariablesRegExp =
'@' . $this->openingDelimiter .
337 "\s*(" . $this->variablenameRegExp .
338 ")\s*" . $this->closingDelimiter .
'@sm';
340 $this->blockRegExp =
'@<!--\s+BEGIN\s+(' . $this->blocknameRegExp .
341 ')\s+-->(.*)<!--\s+END\s+\1\s+-->@sm';
353 if (array_key_exists($option, $this->_options)) {
354 $this->_options[$option] = $value;
368 foreach ($options as $option => $value) {
379 public function show(
string $block = self::IT_DEFAULT_BLOCK): void
381 print $this->
get($block);
390 if ($block === self::IT_DEFAULT_BLOCK && !$this->flagGlobalParsed) {
394 if (!isset($this->blocklist[$block])) {
398 if (isset($this->blockdata[$block])) {
399 $ret = $this->blockdata[$block];
400 if ($this->clearCache) {
401 unset($this->blockdata[$block]);
403 if ($this->_options[
'preserve_data']) {
405 $this->openingDelimiter .
406 '%preserved%' . $this->closingDelimiter,
407 $this->openingDelimiter,
424 public function parse(
string $block = self::IT_DEFAULT_BLOCK,
bool $flag_recursion =
false): bool
426 static $regs, $values;
428 if (!isset($this->blocklist[$block])) {
432 if (self::IT_DEFAULT_BLOCK === $block) {
433 $this->flagGlobalParsed =
true;
436 if (!$flag_recursion) {
440 $outer = $this->blocklist[$block];
443 if ($this->clearCacheOnParse) {
444 foreach ($this->variableCache as $name => $value) {
445 $regs[] = $this->openingDelimiter .
450 $this->variableCache = [];
452 foreach ($this->blockvariables[$block] as $allowedvar => $v) {
453 if (isset($this->variableCache[$allowedvar])) {
454 $regs[] = $this->openingDelimiter .
456 $values[] = $this->variableCache[$allowedvar];
457 unset($this->variableCache[$allowedvar]);
463 if (isset($this->blockinner[$block])) {
464 foreach ($this->blockinner[$block] as $k => $innerblock) {
465 $this->
parse($innerblock,
true);
466 if ($this->blockdata[$innerblock] !==
'') {
470 $placeholder = $this->openingDelimiter .
"__" .
472 $outer = str_replace(
474 $this->blockdata[$innerblock],
477 $this->blockdata[$innerblock] =
"";
481 if (!$flag_recursion && 0 !== count($values)) {
482 if ($this->_options[
'use_preg']) {
490 $funcReplace =
'preg_replace';
492 $funcReplace =
'str_replace';
495 if ($this->_options[
'preserve_data']) {
497 [&$this,
'_preserveOpeningDelimiter'],
502 $outer = $funcReplace($regs, $values, $outer);
504 if ($this->removeUnknownVariables) {
505 $outer = preg_replace($this->removeVariablesRegExp,
"", $outer);
510 if (!$this->removeEmptyBlocks) {
511 $this->blockdata[$block] .= $outer;
512 } elseif (isset($this->touchedBlocks[$block])) {
513 $this->blockdata[$block] .= $outer;
514 unset($this->touchedBlocks[$block]);
516 } elseif (empty($this->blockdata[$block])) {
517 $this->blockdata[$block] = $outer;
519 $this->blockdata[$block] .= $outer;
531 return $this->
parse($this->currentBlock);
546 if (is_array($variable)) {
547 $this->variableCache = array_merge(
548 $this->variableCache,
552 $this->variableCache[$variable] = $value;
563 if (!isset($this->blocklist[$block])) {
567 $this->currentBlock = $block;
578 if (!isset($this->blocklist[$block])) {
582 $this->touchedBlocks[$block] =
true;
594 protected function init(): void
605 $this->block_cache->set($this->real_filename,
$blockdata);
609 $this->
template =
'';
611 if (($blockvariables = $this->variable_cache->get($this->real_filename))!==
null) {
615 $this->variable_cache->set($this->real_filename, $this->blockvariables);
629 $this->variableCache = [];
630 $this->blocklist = [];
631 $this->touchedBlocks = [];
633 $this->flagBlocktrouble =
false;
634 $this->flagGlobalParsed =
false;
651 if (
$template ===
'' && $this->flagCacheTemplatefile) {
652 $this->variableCache = [];
653 $this->blockdata = [];
654 $this->touchedBlocks = [];
658 '<!-- BEGIN ' . self::IT_DEFAULT_BLOCK .
' -->' .
660 '<!-- END ' . self::IT_DEFAULT_BLOCK .
' -->';
664 if ($this->flagBlocktrouble) {
677 bool $removeUnknownVariables =
true,
678 bool $removeEmptyBlocks =
true
681 if (!$this->flagCacheTemplatefile ||
688 return $template !==
'' && $this->setTemplate(
690 $removeUnknownVariables,
703 if ($root !==
'' && substr($root, -1) !==
'/') {
707 $this->fileRoot = $root;
715 foreach ($this->blocklist as $name => $content) {
716 preg_match_all($this->variablesRegExp, $content, $regs);
718 if (count($regs[1]) !== 0) {
719 foreach ($regs[1] as $var) {
720 $this->blockvariables[$name][$var] =
true;
723 $this->blockvariables[$name] = [];
735 if (preg_match_all($this->blockRegExp, $string, $regs, PREG_SET_ORDER)) {
736 foreach ($regs as $match) {
737 $blockname = $match[1];
738 $blockcontent = $match[2];
740 if (isset($this->blocklist[$blockname])) {
744 $this->blocklist[$blockname] = $blockcontent;
745 $this->blockdata[$blockname] =
"";
747 $blocklist[] = $blockname;
749 $inner = $this->findBlocks($blockcontent);
750 foreach ($inner as $name) {
752 '@<!--\s+BEGIN\s+%s\s+-->(.*)<!--\s+END\s+%s\s+-->@sm',
757 $this->blocklist[$blockname] = preg_replace(
759 $this->openingDelimiter .
760 '__' . $name .
'__' .
761 $this->closingDelimiter,
762 $this->blocklist[$blockname]
764 $this->blockinner[$blockname][] = $name;
765 $this->blockparents[$name] = $blockname;
779 if (
$filename[0] ===
'/' && substr($this->fileRoot, -1) ===
'/') {
798 $content = fread($fh, $fsize);
799 $this->template_cache->set(
$filename, $content);
812 return '@' . $str .
'@';
820 return (
false === strpos($str, $this->openingDelimiter)) ?
823 $this->openingDelimiter,
824 $this->openingDelimiter .
825 '%preserved%' . $this->closingDelimiter,
833 public function errorMessage(
int $value,
string $blockname =
''): string
835 static $errorMessages;
836 if (!isset($errorMessages)) {
839 self::IT_ERROR =>
'unknown error',
840 self::IT_TPL_NOT_FOUND =>
'Cannot read the template file',
841 self::IT_BLOCK_NOT_FOUND =>
'Cannot find this block',
842 self::IT_BLOCK_DUPLICATE =>
'The name of a block must be' .
843 ' uniquewithin a template.' .
844 ' Found "' . $blockname .
'" twice.' .
845 'Unpredictable results ' .
847 self::IT_UNKNOWN_OPTION =>
'Unknown option'
851 return $errorMessages[$value] ?? $errorMessages[self::IT_ERROR];
Integrated Template - IT Well there's not much to say about it.
string $closingDelimiter
Last character of a variable placeholder ( {VARIABLE_}_ ).
string $variablenameRegExp
RegExp matching a variable placeholder in the template.
string $blockRegExp
RegExp used to find blocks an their content, filled by the constructor.
bool $flagCacheTemplatefile
EXPERIMENTAL! FIXME! Flag indication that a template gets cached.
free()
Clears all datafields of the object.
array $variableCache
Variable cache.
buildBlockvariablelist()
Build a list of all variables within of a block.
init()
Clears all datafields of the object and rebuild the internal blocklist LoadTemplatefile() and setTemp...
array $blockparents
Array of block parents.
bool $removeEmptyBlocks
Controls the handling of empty blocks, default is remove.
touchBlock(string $block)
Preserves an empty block even if removeEmptyBlocks is true.
setOption(string $option, $value)
Sets the option for the template class.
array $blockinner
Array of inner blocks of a block.
setCurrentBlock(string $block=self::IT_DEFAULT_BLOCK)
Sets the name of the current block that is the block where variables are added.
ILIAS Cache Container Container $block_cache
_addPregDelimiters(string $str)
Adds delimiters to a string, so it can be used as a pattern in preg_* functions.
string $template
Content of the template.
parse(string $block=self::IT_DEFAULT_BLOCK, bool $flag_recursion=false)
Parses the given block.
_preserveOpeningDelimiter(string $str)
Replaces an opening delimiter by a special string.
findBlocks(string $string)
Recusively builds a list of all blocks within the template.
string $fileRoot
Root directory for all file operations.
loadTemplatefile(string $filename, bool $removeUnknownVariables=true, bool $removeEmptyBlocks=true)
Reads a template file from the disk.
array $blockdata
Array with the parsed content of a block.
string $openingDelimiter
First character of a variable placeholder ( _{_VARIABLE} ).
getFile(string $filename)
Reads a file from disk and returns its content.
parseCurrentBlock()
Parses the current block.
string $removeVariablesRegExp
RegExp used to strip unused variable placeholder.
setTemplate(string $template, bool $removeUnknownVariables=true, bool $removeEmptyBlocks=true)
Sets the template.
array $blocklist
Array of all blocks and their content.
array $err
Contains the error objects.
setRoot(string $root)
Sets the file root.
array $blockvariables
Array of variables in a block.
show(string $block=self::IT_DEFAULT_BLOCK)
Print a certain block with all replacements done.
setVariable($variable, $value='')
Sets a variable value.
bool $flagBlocktrouble
Internal flag indicating that a blockname was used multiple times.
bool $flagGlobalParsed
Flag indicating that the global block was parsed.
bool $clearCacheOnParse
Clear the variable cache on parse? If you're not an expert just leave the default false.
bool $clearCache
Clear cache on get()?
ILIAS Cache Container Container $template_cache
string $currentBlock
Name of the current block.
array $_options
$_options['preserve_data'] Whether to substitute variables and remove empty placeholders in data pass...
__construct(string $root='', ?array $options=null)
Builds some complex regular expressions and optinally sets the file root directory.
string $variablesRegExp
RegExp used to find variable placeholder, filled by the constructor.
string $blocknameRegExp
RegExp matching a block in the template.
string $lastTemplatefile
EXPERIMENTAL! FIXME!
setOptions(array $options)
Sets the options for the template class.
string $real_filename
Holds the real template file name.
array $touchedBlocks
List of blocks to preverse even if they are "empty".
bool $removeUnknownVariables
Controls the handling of unknown variables, default is remove.
errorMessage(int $value, string $blockname='')
Return a textual error message for a IT error code.
ILIAS Cache Container Container $variable_cache
Customizing of pimple-DIC for ILIAS.