19 declare(strict_types=1);
43 require_once __DIR__ .
'/../../exceptions/class.ilTemplateException.php';
306 'preserve_data' =>
false,
324 public function __construct(
string $root =
'', array $options = null)
328 $this->block_cache = $DIC->globalCache()->get(
new BlockCache());
329 $this->variable_cache = $DIC->globalCache()->get(
new VariableCache());
330 $this->template_cache = $DIC->globalCache()->get(
new TemplateCache());
332 if (!is_null($options)) {
335 $this->variablesRegExp =
'@' . $this->openingDelimiter .
336 '(' . $this->variablenameRegExp .
')' .
337 $this->closingDelimiter .
'@sm';
338 $this->removeVariablesRegExp =
'@' . $this->openingDelimiter .
339 "\s*(" . $this->variablenameRegExp .
340 ")\s*" . $this->closingDelimiter .
'@sm';
342 $this->blockRegExp =
'@<!--\s+BEGIN\s+(' . $this->blocknameRegExp .
343 ')\s+-->(.*)<!--\s+END\s+\1\s+-->@sm';
355 if (array_key_exists($option, $this->_options)) {
356 $this->_options[$option] = $value;
370 foreach ($options as $option => $value) {
381 public function show(
string $block = self::IT_DEFAULT_BLOCK): void
383 print $this->
get($block);
390 public function get(
string $block = self::IT_DEFAULT_BLOCK):
string 392 if ($block === self::IT_DEFAULT_BLOCK && !$this->flagGlobalParsed) {
396 if (!isset($this->blocklist[$block])) {
400 if (isset($this->blockdata[$block])) {
401 $ret = $this->blockdata[$block];
402 if ($this->clearCache) {
403 unset($this->blockdata[$block]);
405 if ($this->_options[
'preserve_data']) {
407 $this->openingDelimiter .
408 '%preserved%' . $this->closingDelimiter,
409 $this->openingDelimiter,
426 public function parse(
string $block = self::IT_DEFAULT_BLOCK,
bool $flag_recursion =
false): bool
428 static $regs, $values;
430 if (!isset($this->blocklist[$block])) {
434 if (self::IT_DEFAULT_BLOCK === $block) {
435 $this->flagGlobalParsed =
true;
438 if (!$flag_recursion) {
442 $outer = $this->blocklist[$block];
445 if ($this->clearCacheOnParse) {
446 foreach ($this->variableCache as $name => $value) {
447 $regs[] = $this->openingDelimiter .
452 $this->variableCache = [];
454 foreach ($this->blockvariables[$block] as $allowedvar => $v) {
455 if (isset($this->variableCache[$allowedvar])) {
456 $regs[] = $this->openingDelimiter .
458 $values[] = $this->variableCache[$allowedvar];
459 unset($this->variableCache[$allowedvar]);
465 if (isset($this->blockinner[$block])) {
466 foreach ($this->blockinner[$block] as $k => $innerblock) {
467 $this->
parse($innerblock,
true);
468 if ($this->blockdata[$innerblock] !==
'') {
472 $placeholder = $this->openingDelimiter .
"__" .
474 $outer = str_replace(
476 $this->blockdata[$innerblock],
479 $this->blockdata[$innerblock] =
"";
483 if (!$flag_recursion && 0 !== count($values)) {
484 if ($this->_options[
'use_preg']) {
492 $funcReplace =
'preg_replace';
494 $funcReplace =
'str_replace';
497 if ($this->_options[
'preserve_data']) {
499 [&$this,
'_preserveOpeningDelimiter'],
504 $outer = $funcReplace($regs, $values, $outer);
506 if ($this->removeUnknownVariables) {
507 $outer = preg_replace($this->removeVariablesRegExp,
"", $outer);
512 if (!$this->removeEmptyBlocks) {
513 $this->blockdata[$block] .= $outer;
514 } elseif (isset($this->touchedBlocks[$block])) {
515 $this->blockdata[$block] .= $outer;
516 unset($this->touchedBlocks[$block]);
518 } elseif (empty($this->blockdata[$block])) {
519 $this->blockdata[$block] = $outer;
521 $this->blockdata[$block] .= $outer;
533 return $this->
parse($this->currentBlock);
548 if (is_array($variable)) {
549 $this->variableCache = array_merge(
550 $this->variableCache,
554 $this->variableCache[$variable] = $value;
565 if (!isset($this->blocklist[$block])) {
569 $this->currentBlock = $block;
580 if (!isset($this->blocklist[$block])) {
584 $this->touchedBlocks[$block] =
true;
596 protected function init(): void
601 $this->blockdata = $blockdata[
'blockdata'];
602 $this->blocklist = $blockdata[
'blocklist'];
607 $this->block_cache->set($this->real_filename, $blockdata);
611 $this->
template =
'';
613 if (($blockvariables = $this->variable_cache->get($this->real_filename))!==null) {
617 $this->variable_cache->set($this->real_filename, $this->blockvariables);
629 $this->currentBlock = self::IT_DEFAULT_BLOCK;
631 $this->variableCache = [];
632 $this->blocklist = [];
633 $this->touchedBlocks = [];
635 $this->flagBlocktrouble =
false;
636 $this->flagGlobalParsed =
false;
647 bool $removeUnknownVariables =
true,
648 bool $removeEmptyBlocks =
true 653 if ($template ===
'' && $this->flagCacheTemplatefile) {
654 $this->variableCache = [];
655 $this->blockdata = [];
656 $this->touchedBlocks = [];
657 $this->currentBlock = self::IT_DEFAULT_BLOCK;
660 '<!-- BEGIN ' . self::IT_DEFAULT_BLOCK .
' -->' .
662 '<!-- END ' . self::IT_DEFAULT_BLOCK .
' -->';
666 if ($this->flagBlocktrouble) {
679 bool $removeUnknownVariables =
true,
680 bool $removeEmptyBlocks =
true 683 if (!$this->flagCacheTemplatefile ||
684 $this->lastTemplatefile !== $filename
686 $template = $this->
getFile($filename);
692 $removeUnknownVariables,
705 if ($root !==
'' && substr($root, -1) !==
'/') {
709 $this->fileRoot = $root;
717 foreach ($this->blocklist as $name => $content) {
718 preg_match_all($this->variablesRegExp, $content, $regs);
720 if (count($regs[1]) !== 0) {
721 foreach ($regs[1] as $var) {
722 $this->blockvariables[$name][$var] =
true;
725 $this->blockvariables[$name] = [];
737 if (preg_match_all($this->blockRegExp, $string, $regs, PREG_SET_ORDER)) {
738 foreach ($regs as $match) {
739 $blockname = $match[1];
740 $blockcontent = $match[2];
742 if (isset($this->blocklist[$blockname])) {
746 $this->blocklist[$blockname] = $blockcontent;
747 $this->blockdata[$blockname] =
"";
749 $blocklist[] = $blockname;
752 foreach ($inner as $name) {
754 '@<!--\s+BEGIN\s+%s\s+-->(.*)<!--\s+END\s+%s\s+-->@sm',
759 $this->blocklist[$blockname] = preg_replace(
761 $this->openingDelimiter .
762 '__' . $name .
'__' .
763 $this->closingDelimiter,
764 $this->blocklist[$blockname]
766 $this->blockinner[$blockname][] = $name;
767 $this->blockparents[$name] = $blockname;
781 if ($filename[0] ===
'/' && substr($this->fileRoot, -1) ===
'/') {
782 $filename = substr($filename, 1);
790 if (!($fh = @fopen($filename,
'rb'))) {
794 $fsize = filesize($filename);
800 $content = fread($fh, $fsize);
801 $this->template_cache->set($filename, $content);
814 return '@' . $str .
'@';
822 return (
false === strpos($str, $this->openingDelimiter)) ?
825 $this->openingDelimiter,
826 $this->openingDelimiter .
827 '%preserved%' . $this->closingDelimiter,
835 public function errorMessage(
int $value,
string $blockname =
''): string
837 static $errorMessages;
838 if (!isset($errorMessages)) {
841 self::IT_ERROR =>
'unknown error',
842 self::IT_TPL_NOT_FOUND =>
'Cannot read the template file',
843 self::IT_BLOCK_NOT_FOUND =>
'Cannot find this block',
844 self::IT_BLOCK_DUPLICATE =>
'The name of a block must be' .
845 ' uniquewithin a template.' .
846 ' Found "' . $blockname .
'" twice.' .
847 'Unpredictable results ' .
849 self::IT_UNKNOWN_OPTION =>
'Unknown option' 853 return $errorMessages[$value] ?? $errorMessages[self::IT_ERROR];
string $openingDelimiter
First character of a variable placeholder ( _{_VARIABLE} ).
string $closingDelimiter
Last character of a variable placeholder ( {VARIABLE_}_ ).
setOptions(array $options)
Sets the options for the template class.
setTemplate(string $template, bool $removeUnknownVariables=true, bool $removeEmptyBlocks=true)
Sets the template.
bool $removeEmptyBlocks
Controls the handling of empty blocks, default is remove.
string $currentBlock
Name of the current block.
array $_options
$_options['preserve_data'] Whether to substitute variables and remove empty placeholders in data pass...
string $template
Content of the template.
array $blockparents
Array of block parents.
array $blockvariables
Array of variables in a block.
string $variablesRegExp
RegExp used to find variable placeholder, filled by the constructor.
bool $clearCacheOnParse
Clear the variable cache on parse? If you're not an expert just leave the default false...
string $removeVariablesRegExp
RegExp used to strip unused variable placeholder.
bool $flagCacheTemplatefile
EXPERIMENTAL! FIXME! Flag indication that a template gets cached.
string $fileRoot
Root directory for all file operations.
bool $flagBlocktrouble
Internal flag indicating that a blockname was used multiple times.
Customizing of pimple-DIC for ILIAS.
bool $removeUnknownVariables
Controls the handling of unknown variables, default is remove.
findBlocks(string $string)
Recusively builds a list of all blocks within the template.
free()
Clears all datafields of the object.
string $blocknameRegExp
RegExp matching a block in the template.
setOption(string $option, $value)
Sets the option for the template class.
string $lastTemplatefile
EXPERIMENTAL! FIXME!
setVariable($variable, $value='')
Sets a variable value.
show(string $block=self::IT_DEFAULT_BLOCK)
Print a certain block with all replacements done.
bool $flagGlobalParsed
Flag indicating that the global block was parsed.
getFile(string $filename)
Reads a file from disk and returns its content.
touchBlock(string $block)
Preserves an empty block even if removeEmptyBlocks is true.
string $blockRegExp
RegExp used to find blocks an their content, filled by the constructor.
_addPregDelimiters(string $str)
Adds delimiters to a string, so it can be used as a pattern in preg_* functions.
errorMessage(int $value, string $blockname='')
Return a textual error message for a IT error code.
Integrated Template - IT Well there's not much to say about it.
array string $real_filename
Holds the real template file name.
_preserveOpeningDelimiter(string $str)
Replaces an opening delimiter by a special string.
bool $clearCache
Clear cache on get()?
parseCurrentBlock()
Parses the current block.
buildBlockvariablelist()
Build a list of all variables within of a block.
ILIAS Cache Container Container $variable_cache
setCurrentBlock(string $block=self::IT_DEFAULT_BLOCK)
Sets the name of the current block that is the block where variables are added.
setRoot(string $root)
Sets the file root.
ILIAS Cache Container Container $block_cache
string $variablenameRegExp
RegExp matching a variable placeholder in the template.
array $touchedBlocks
List of blocks to preverse even if they are "empty".
init()
Clears all datafields of the object and rebuild the internal blocklist LoadTemplatefile() and setTemp...
loadTemplatefile(string $filename, bool $removeUnknownVariables=true, bool $removeEmptyBlocks=true)
Reads a template file from the disk.
parse(string $block=self::IT_DEFAULT_BLOCK, bool $flag_recursion=false)
Parses the given block.
array $blockinner
Array of inner blocks of a block.
array $err
Contains the error objects.
array $blockdata
Array with the parsed content of a block.
array $variableCache
Variable cache.
array $blocklist
Array of all blocks and their content.
ILIAS Cache Container Container $template_cache
__construct(string $root='', array $options=null)
Builds some complex regular expressions and optinally sets the file root directory.