3 declare(strict_types=1);
20 require_once __DIR__ .
'/../../exceptions/class.ilTemplateException.php';
280 'preserve_data' =>
false,
298 public function __construct(
string $root =
'', array $options = null)
300 if (!is_null($options)) {
303 $this->variablesRegExp =
'@' . $this->openingDelimiter .
304 '(' . $this->variablenameRegExp .
')' .
305 $this->closingDelimiter .
'@sm';
306 $this->removeVariablesRegExp =
'@' . $this->openingDelimiter .
307 "\s*(" . $this->variablenameRegExp .
308 ")\s*" . $this->closingDelimiter .
'@sm';
310 $this->blockRegExp =
'@<!--\s+BEGIN\s+(' . $this->blocknameRegExp .
311 ')\s+-->(.*)<!--\s+END\s+\1\s+-->@sm';
323 if (array_key_exists($option, $this->_options)) {
324 $this->_options[$option] = $value;
338 foreach ($options as $option => $value) {
349 public function show(
string $block = self::IT_DEFAULT_BLOCK): void
351 print $this->
get($block);
358 public function get(
string $block = self::IT_DEFAULT_BLOCK):
string 360 if ($block === self::IT_DEFAULT_BLOCK && !$this->flagGlobalParsed) {
364 if (!isset($this->blocklist[$block])) {
368 if (isset($this->blockdata[$block])) {
369 $ret = $this->blockdata[$block];
370 if ($this->clearCache) {
371 unset($this->blockdata[$block]);
373 if ($this->_options[
'preserve_data']) {
375 $this->openingDelimiter .
376 '%preserved%' . $this->closingDelimiter,
377 $this->openingDelimiter,
394 public function parse(
string $block = self::IT_DEFAULT_BLOCK,
bool $flag_recursion =
false): bool
396 static $regs, $values;
398 if (!isset($this->blocklist[$block])) {
402 if (self::IT_DEFAULT_BLOCK === $block) {
403 $this->flagGlobalParsed =
true;
406 if (!$flag_recursion) {
410 $outer = $this->blocklist[$block];
413 if ($this->clearCacheOnParse) {
414 foreach ($this->variableCache as
$name => $value) {
415 $regs[] = $this->openingDelimiter .
420 $this->variableCache = [];
422 foreach ($this->blockvariables[$block] as $allowedvar => $v) {
423 if (isset($this->variableCache[$allowedvar])) {
424 $regs[] = $this->openingDelimiter .
426 $values[] = $this->variableCache[$allowedvar];
427 unset($this->variableCache[$allowedvar]);
433 if (isset($this->blockinner[$block])) {
434 foreach ($this->blockinner[$block] as $k => $innerblock) {
435 $this->
parse($innerblock,
true);
436 if ($this->blockdata[$innerblock] !==
'') {
440 $placeholder = $this->openingDelimiter .
"__" .
442 $outer = str_replace(
444 $this->blockdata[$innerblock],
447 $this->blockdata[$innerblock] =
"";
451 if (!$flag_recursion && 0 !== count($values)) {
452 if ($this->_options[
'use_preg']) {
460 $funcReplace =
'preg_replace';
462 $funcReplace =
'str_replace';
465 if ($this->_options[
'preserve_data']) {
467 [&$this,
'_preserveOpeningDelimiter'],
472 $outer = $funcReplace($regs, $values, $outer);
474 if ($this->removeUnknownVariables) {
475 $outer = preg_replace($this->removeVariablesRegExp,
"", $outer);
480 if (!$this->removeEmptyBlocks) {
481 $this->blockdata[$block] .= $outer;
482 } elseif (isset($this->touchedBlocks[$block])) {
483 $this->blockdata[$block] .= $outer;
484 unset($this->touchedBlocks[$block]);
486 } elseif (empty($this->blockdata[$block])) {
487 $this->blockdata[$block] = $outer;
489 $this->blockdata[$block] .= $outer;
501 return $this->
parse($this->currentBlock);
516 if (is_array($variable)) {
517 $this->variableCache = array_merge(
518 $this->variableCache,
522 $this->variableCache[$variable] = $value;
533 if (!isset($this->blocklist[$block])) {
537 $this->currentBlock = $block;
548 if (!isset($this->blocklist[$block])) {
552 $this->touchedBlocks[$block] =
true;
564 protected function init(): void
569 if ($blockdata = $blocks->get($this->real_filename)) {
570 $this->blockdata = $blockdata[
'blockdata'];
571 $this->blocklist = $blockdata[
'blocklist'];
577 $blocks->set($this->real_filename, $blockdata, 60);
581 $this->
template =
'';
584 if ($blockvariables = $variables->get($this->real_filename)) {
588 $variables->set($this->real_filename, $this->blockvariables, 60);
600 $this->currentBlock = self::IT_DEFAULT_BLOCK;
602 $this->variableCache = [];
603 $this->blocklist = [];
604 $this->touchedBlocks = [];
606 $this->flagBlocktrouble =
false;
607 $this->flagGlobalParsed =
false;
618 bool $removeUnknownVariables =
true,
619 bool $removeEmptyBlocks =
true 624 if ($template ===
'' && $this->flagCacheTemplatefile) {
625 $this->variableCache = [];
626 $this->blockdata = [];
627 $this->touchedBlocks = [];
628 $this->currentBlock = self::IT_DEFAULT_BLOCK;
631 '<!-- BEGIN ' . self::IT_DEFAULT_BLOCK .
' -->' .
633 '<!-- END ' . self::IT_DEFAULT_BLOCK .
' -->';
637 if ($this->flagBlocktrouble) {
650 bool $removeUnknownVariables =
true,
651 bool $removeEmptyBlocks =
true 654 if (!$this->flagCacheTemplatefile ||
655 $this->lastTemplatefile !== $filename
657 $template = $this->
getFile($filename);
663 $removeUnknownVariables,
676 if ($root !==
'' && substr($root, -1) !==
'/') {
680 $this->fileRoot = $root;
688 foreach ($this->blocklist as
$name => $content) {
689 preg_match_all($this->variablesRegExp, $content, $regs);
691 if (count($regs[1]) !== 0) {
692 foreach ($regs[1] as $var) {
693 $this->blockvariables[
$name][$var] =
true;
696 $this->blockvariables[
$name] = [];
708 if (preg_match_all($this->blockRegExp, $string, $regs, PREG_SET_ORDER)) {
709 foreach ($regs as $match) {
710 $blockname = $match[1];
711 $blockcontent = $match[2];
713 if (isset($this->blocklist[$blockname])) {
717 $this->blocklist[$blockname] = $blockcontent;
718 $this->blockdata[$blockname] =
"";
720 $blocklist[] = $blockname;
723 foreach ($inner as
$name) {
725 '@<!--\s+BEGIN\s+%s\s+-->(.*)<!--\s+END\s+%s\s+-->@sm',
730 $this->blocklist[$blockname] = preg_replace(
732 $this->openingDelimiter .
733 '__' . $name .
'__' .
734 $this->closingDelimiter,
735 $this->blocklist[$blockname]
737 $this->blockinner[$blockname][] =
$name;
738 $this->blockparents[
$name] = $blockname;
752 if ($filename[0] ===
'/' && substr($this->fileRoot, -1) ===
'/') {
753 $filename = substr($filename, 1);
760 if (!$content = $ilGlobalCache->get($filename)) {
761 if (!($fh = @fopen($filename,
'rb'))) {
765 $fsize = filesize($filename);
771 $content = fread($fh, $fsize);
772 $ilGlobalCache->set($filename, $content, 60);
785 return '@' . $str .
'@';
793 return (
false === strpos($str, $this->openingDelimiter)) ?
796 $this->openingDelimiter,
797 $this->openingDelimiter .
798 '%preserved%' . $this->closingDelimiter,
806 public function errorMessage(
int $value,
string $blockname =
''): string
808 static $errorMessages;
809 if (!isset($errorMessages)) {
812 self::IT_ERROR =>
'unknown error',
813 self::IT_TPL_NOT_FOUND =>
'Cannot read the template file',
814 self::IT_BLOCK_NOT_FOUND =>
'Cannot find this block',
815 self::IT_BLOCK_DUPLICATE =>
'The name of a block must be' .
816 ' uniquewithin a template.' .
817 ' Found "' . $blockname .
'" twice.' .
818 'Unpredictable results ' .
820 self::IT_UNKNOWN_OPTION =>
'Unknown option' 824 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...
static log(string $message, int $log_level)
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.
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.
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.
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.
static getInstance(?string $component)
array $blocklist
Array of all blocks and their content.
__construct(string $root='', array $options=null)
Builds some complex regular expressions and optinally sets the file root directory.