19 declare(strict_types=1);
101 $this->checkblocknameRegExp =
'@' . $this->blocknameRegExp .
'@';
102 $this->functionRegExp =
'@' . $this->functionPrefix .
'(' .
103 $this->functionnameRegExp .
')\s*\(@sm';
108 protected function init(): void
114 $this->
template =
'';
136 if (!isset($this->blocklist[$block])) {
137 throw new ilTemplateException(
"The block " .
"'$block'" .
" does not exist in the template and thus it can't be replaced.");
140 if ($template ===
'') {
151 $template =
"<!-- BEGIN $block -->" . $template .
"<!-- END $block -->";
152 $parents = $this->blockparents[$block];
154 $this->blockparents[$block] = $parents;
189 if ($placeholder ===
'') {
193 if ($blockname ===
'' ||
194 !preg_match($this->checkblocknameRegExp, $blockname)) {
198 if ($template ===
'') {
202 if (isset($this->blocklist[$blockname])) {
208 if (count($parents) === 0) {
210 " '$placeholder' was not found in the template."));
213 if (count($parents) > 1) {
215 foreach ($parents as $index => $parent) {
216 $msg .= (isset($parents[$index + 1])) ?
217 "$parent, " : $parent;
220 throw new ilTemplateException(
"The variable placeholder " .
"'$placeholder'" .
" must be unique, found in multiple blocks '$msg'.");
223 $template =
"<!-- BEGIN $blockname -->" . $template .
"<!-- END $blockname -->";
225 if ($this->flagBlocktrouble) {
228 $this->blockinner[$parents[0]][] = $blockname;
229 $this->blocklist[$parents[0]] = preg_replace(
230 '@' . $this->openingDelimiter . $placeholder .
231 $this->closingDelimiter .
'@',
232 $this->openingDelimiter .
'__' . $blockname .
'__' .
233 $this->closingDelimiter,
234 $this->blocklist[$parents[0]]
250 return $this->
addBlock($placeholder, $blockname, $this->
getFile($filename));
258 if (isset($this->blockinner[$block])) {
259 foreach ($this->blockinner[$block] as $inner) {
263 unset($this->blockinner[$block]);
267 $this->blocklist[$block],
268 $this->blockdata[$block],
269 $this->blockvariables[$block],
270 $this->touchedBlocks[$block]
279 return isset($this->blocklist[$blockname]);
287 $this->functions = [];
292 while (preg_match($this->functionRegExp,
$template, $regs)) {
299 $search = $regs[0] . $head .
')';
301 $replace = $this->openingDelimiter .
302 '__function' . $num .
'__' .
305 $this->
template = str_replace($search, $replace, $this->
template);
308 while ($head !==
'' && $args2 = $this->
getValue($head,
',')) {
309 $arg2 = trim($args2);
310 $args[] = (
'"' === $arg2[0] ||
"'" === $arg2[0]) ?
311 substr($arg2, 1, -1) : $arg2;
312 if ($arg2 === $head) {
315 $head = substr($head, strlen($arg2) + 1);
318 $this->functions[$num++] = [
330 public function getValue(
string $code, $delimiter): string
336 if (!is_array($delimiter)) {
337 $delimiter = [$delimiter =>
true];
340 $len = strlen($code);
344 if (isset($delimiter[$code[0]])) {
347 for ($i = 0; $i < $len; ++$i) {
351 ($char ===
'"' || $char ===
"'") &&
352 ($char === $enclosed_by ||
'' === $enclosed_by) &&
353 (0 === $i || ($i > 0 &&
'\\' !== $code[$i - 1]))
356 $enclosed_by = $char;
360 $enclosed = !$enclosed;
363 if (!$enclosed && isset($delimiter[$char])) {
369 return substr($code, 0, $i);
379 if (!is_array($variables)) {
380 $variables = [$variables =>
true];
383 reset($this->blockvariables[$block]);
384 foreach ($this->blockvariables[$block] as $varname => $val) {
385 if (isset($variables[$varname])) {
386 unset($this->blockvariables[$block][$varname]);
397 $this->variablesRegExp,
398 $this->blocklist[$block],
402 if (count($regs[1]) !== 0) {
403 foreach ($regs[1] as $var) {
404 $this->blockvariables[$block][$var] =
true;
407 $this->blockvariables[$block] = [];
411 if (isset($this->blockinner[$block]) &&
412 is_array($this->blockinner[$block]) &&
413 count($this->blockinner[$block]) > 0
419 foreach ($this->blockinner[$block] as $childBlock) {
432 reset($this->blocklist);
433 foreach ($this->blocklist as $blockname => $content) {
434 reset($this->blockvariables[$blockname]);
435 foreach ($this->blockvariables[$blockname] as $varname => $val) {
436 if ($variable === $varname) {
437 $parents[] = $blockname;
452 'HTML_Template_ITX Warning: %s [File: %s, Line: %d]',
460 if ($this->printWarning) {
464 if ($this->haltOnWarning) {
string $functionRegExp
RegExp used to grep function calls in the template.
string $closingDelimiter
Last character of a variable placeholder ( {VARIABLE_}_ ).
string $functionnameRegExp
Functionname RegExp.
string $template
Content of the template.
string $functionPrefix
Functionnameprefix used when searching function calls in the template.
removeBlockData(string $block)
Recursively removes all data assiciated with a block, including all inner blocks. ...
replaceBlock(string $block, string $template, bool $keep_content=false)
Replaces an existing block with new content.
Integrated Template Extension - ITX With this class you get the full power of the phplib template cla...
findBlocks(string $string)
Recusively builds a list of all blocks within the template.
free()
Clears all datafields of the object.
array $callback
List of callback functions specified by the user.
warning(string $message, string $file='', int $line=0)
Handles warnings, saves them to $warn and prints them or calls die() depending on the flags...
updateBlockvariablelist(string $block)
Updates the variable list of a block.
bool $printWarning
Print warnings?
__construct(string $root='')
Builds some complex regexps and calls the constructor of the parent class.
getFile(string $filename)
Reads a file from disk and returns its content.
Integrated Template - IT Well there's not much to say about it.
addBlockfile(string $placeholder, string $blockname, string $filename)
Adds a block taken from a file to the template changing a variable placeholder to a block placeholder...
buildBlockvariablelist()
Build a list of all variables within of a block.
__construct(Container $dic, ilPlugin $plugin)
array $warn
Array with all warnings.
findPlaceholderBlocks(string $variable)
Returns an array of blocknames where the given variable placeholder is used.
array $blockdata
Array with the parsed content of a block.
array $functions
List of functions found in the template.
deleteFromBlockvariablelist(string $block, $variables)
Deletes one or many variables from the block variable list.
blockExists(string $blockname)
Checks wheter a block exists.
bool $haltOnWarning
Call die() on warning?
addBlock(string $placeholder, string $blockname, string $template)
Adds a block to the template changing a variable placeholder to a block placeholder.
string $checkblocknameRegExp
RegExp used to test for a valid blockname.
buildFunctionlist()
Builds a functionlist from the template.
getValue(string $code, $delimiter)
Truncates the given code from the first occurence of $delimiter but ignores $delimiter enclosed by " ...