ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
HTML_Template_IT Class Reference

Integrated Template - IT Well there's not much to say about it. More...

+ Inheritance diagram for HTML_Template_IT:
+ Collaboration diagram for HTML_Template_IT:

Public Member Functions

 __construct (string $root='', ?array $options=null)
 Builds some complex regular expressions and optinally sets the file root directory. More...
 
 setOption (string $option, $value)
 Sets the option for the template class. More...
 
 setOptions (array $options)
 Sets the options for the template class. More...
 
 show (string $block=self::IT_DEFAULT_BLOCK)
 Print a certain block with all replacements done. More...
 
 get (string $block=self::IT_DEFAULT_BLOCK)
 Returns a block with all replacements done. More...
 
 parse (string $block=self::IT_DEFAULT_BLOCK, bool $flag_recursion=false)
 Parses the given block. More...
 
 parseCurrentBlock ()
 Parses the current block. More...
 
 setVariable ($variable, $value='')
 Sets a variable value. More...
 
 setCurrentBlock (string $block=self::IT_DEFAULT_BLOCK)
 Sets the name of the current block that is the block where variables are added. More...
 
 touchBlock (string $block)
 Preserves an empty block even if removeEmptyBlocks is true. More...
 
 free ()
 Clears all datafields of the object. More...
 
 setTemplate (string $template, bool $removeUnknownVariables=true, bool $removeEmptyBlocks=true)
 Sets the template. More...
 
 loadTemplatefile (string $filename, bool $removeUnknownVariables=true, bool $removeEmptyBlocks=true)
 Reads a template file from the disk. More...
 
 setRoot (string $root)
 Sets the file root. More...
 
 buildBlockvariablelist ()
 Build a list of all variables within of a block. More...
 
 findBlocks (string $string)
 Recusively builds a list of all blocks within the template. More...
 
 getFile (string $filename)
 Reads a file from disk and returns its content. More...
 
 _addPregDelimiters (string $str)
 Adds delimiters to a string, so it can be used as a pattern in preg_* functions. More...
 
 _preserveOpeningDelimiter (string $str)
 Replaces an opening delimiter by a special string. More...
 
 errorMessage (int $value, string $blockname='')
 Return a textual error message for a IT error code. More...
 

Data Fields

const IT_OK = 1
 
const IT_ERROR = -1
 
const IT_TPL_NOT_FOUND = -2
 
const IT_BLOCK_NOT_FOUND = -3
 
const IT_BLOCK_DUPLICATE = -4
 
const IT_UNKNOWN_OPTION = -6
 
const IT_DEFAULT_BLOCK = '__global__'
 
array $err = []
 Contains the error objects. More...
 
bool $clearCache = false
 Clear cache on get()? More...
 
string $openingDelimiter = '{'
 First character of a variable placeholder ( _{_VARIABLE} ). More...
 
string $closingDelimiter = '}'
 Last character of a variable placeholder ( {VARIABLE_}_ ). More...
 
string $blocknameRegExp = '[\.0-9A-Za-z_-]+'
 RegExp matching a block in the template. More...
 
string $variablenameRegExp = '[\.0-9A-Za-z_-]+'
 RegExp matching a variable placeholder in the template. More...
 
string $variablesRegExp = ''
 RegExp used to find variable placeholder, filled by the constructor. More...
 
string $removeVariablesRegExp = ''
 RegExp used to strip unused variable placeholder. More...
 
bool $removeUnknownVariables = true
 Controls the handling of unknown variables, default is remove. More...
 
bool $removeEmptyBlocks = true
 Controls the handling of empty blocks, default is remove. More...
 
string $blockRegExp = ''
 RegExp used to find blocks an their content, filled by the constructor. More...
 
string $currentBlock = self::IT_DEFAULT_BLOCK
 Name of the current block. More...
 
string $template = ''
 Content of the template. More...
 
array $blocklist = []
 Array of all blocks and their content. More...
 
array $blockdata = []
 Array with the parsed content of a block. More...
 
array $blockvariables = []
 Array of variables in a block. More...
 
array $blockparents = []
 Array of block parents. More...
 
array $blockinner = []
 Array of inner blocks of a block. More...
 
array $touchedBlocks = []
 List of blocks to preverse even if they are "empty". More...
 
array $variableCache = []
 Variable cache. More...
 
bool $clearCacheOnParse = false
 Clear the variable cache on parse? If you're not an expert just leave the default false. More...
 
string $fileRoot = ''
 Root directory for all file operations. More...
 
bool $flagBlocktrouble = false
 Internal flag indicating that a blockname was used multiple times. More...
 
bool $flagGlobalParsed = false
 Flag indicating that the global block was parsed. More...
 
bool $flagCacheTemplatefile = true
 EXPERIMENTAL! FIXME! Flag indication that a template gets cached. More...
 
string $lastTemplatefile = ''
 EXPERIMENTAL! FIXME! More...
 
array $_options
 $_options['preserve_data'] Whether to substitute variables and remove empty placeholders in data passed through setVariable (see also bugs #20199, #21951). More...
 

Protected Member Functions

 init ()
 Clears all datafields of the object and rebuild the internal blocklist LoadTemplatefile() and setTemplate() automatically call this function when a new template is given. More...
 

Protected Attributes

string $real_filename = ''
 Holds the real template file name. More...
 

Private Attributes

ILIAS Cache Container Container $block_cache
 
ILIAS Cache Container Container $variable_cache
 
ILIAS Cache Container Container $template_cache
 

Detailed Description

Integrated Template - IT Well there's not much to say about it.

I needed a template class that supports a single template file with multiple (nested) blocks inside and a simple block API. The Isotemplate API is somewhat tricky for a beginner although it is the best one you can build. template::parse() [phplib template = Isotemplate] requests you to name a source and a target where the current block gets parsed into. Source and target can be block names or even handler names. This API gives you a maximum of fexibility but you always have to know what you do which is quite unusual for php skripter like me. I noticed that I do not any control on which block gets parsed into which one. If all blocks are within one file, the script knows how they are nested and in which way you have to parse them. IT knows that inner1 is a child of block2, there's no need to tell him about this.

global

(hidden and automatically added)

block1
block2
inner1 inner2

To add content to block1 you simply type: $tpl->setCurrentBlock("block1"); and repeat this as often as needed: $tpl->setVariable(...); $tpl->parseCurrentBlock(); To add content to block2 you would type something like: $tpl->setCurrentBlock("inner1"); $tpl->setVariable(...); $tpl->parseCurrentBlock(); $tpl->setVariable(...); $tpl->parseCurrentBlock(); $tpl->parse("block1"); This will result in one repition of block1 which contains two repitions of inner1. inner2 will be removed if $removeEmptyBlock is set to true which is the default. Usage: $tpl = new HTML_Template_IT( [string filerootdir] ); // load a template or set it with setTemplate() $tpl->loadTemplatefile( string filename [, boolean removeUnknownVariables, boolean removeEmptyBlocks] ) // set "global" Variables meaning variables not beeing within a (inner) block $tpl->setVariable( string variablename, mixed value ); // like with the Isotemplates there's a second way to use setVariable() $tpl->setVariable( array ( string varname => mixed value ) ); // Let's use any block, even a deeply nested one $tpl->setCurrentBlock( string blockname ); // repeat this as often as you need it. $tpl->setVariable( array ( string varname => mixed value ) ); $tpl->parseCurrentBlock(); // get the parsed template or print it: $tpl->show() $tpl->get();

Author
Ulf Wendel uw@ne.nosp@m.tuse.nosp@m..de

Definition at line 118 of file IT.php.

Constructor & Destructor Documentation

◆ __construct()

HTML_Template_IT::__construct ( string  $root = '',
?array  $options = null 
)

Builds some complex regular expressions and optinally sets the file root directory.

Make sure that you call this constructor if you derive your template class from this one.

Parameters
string$rootFile root directory, prefix for all filenames given to the object.
string[]$optionsarray of options.
Exceptions
ilTemplateException

Definition at line 322 of file IT.php.

323 {
324 global $DIC;
325 $DIC = $DIC instanceof Container ? $DIC : new Container([]);
326 $this->block_cache = $DIC->globalCache()->get(new BlockCache());
327 $this->variable_cache = $DIC->globalCache()->get(new VariableCache());
328 $this->template_cache = $DIC->globalCache()->get(new TemplateCache());
329
330 if (!is_null($options)) {
331 $this->setOptions($options);
332 }
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';
339
340 $this->blockRegExp = '@<!--\s+BEGIN\s+(' . $this->blocknameRegExp .
341 ')\s+-->(.*)<!--\s+END\s+\1\s+-->@sm';
342
343 $this->setRoot($root);
344 }
setRoot(string $root)
Sets the file root.
Definition: IT.php:701
setOptions(array $options)
Sets the options for the template class.
Definition: IT.php:366
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
global $DIC
Definition: shib_login.php:26

References $DIC, setOptions(), and setRoot().

+ Here is the call graph for this function:

Member Function Documentation

◆ _addPregDelimiters()

HTML_Template_IT::_addPregDelimiters ( string  $str)

Adds delimiters to a string, so it can be used as a pattern in preg_* functions.

Definition at line 810 of file IT.php.

810 : string
811 {
812 return '@' . $str . '@';
813 }

◆ _preserveOpeningDelimiter()

HTML_Template_IT::_preserveOpeningDelimiter ( string  $str)

Replaces an opening delimiter by a special string.

Definition at line 818 of file IT.php.

818 : string
819 {
820 return (false === strpos($str, $this->openingDelimiter)) ?
821 $str :
822 str_replace(
823 $this->openingDelimiter,
824 $this->openingDelimiter .
825 '%preserved%' . $this->closingDelimiter,
826 $str
827 );
828 }

◆ buildBlockvariablelist()

HTML_Template_IT::buildBlockvariablelist ( )

Build a list of all variables within of a block.

Definition at line 713 of file IT.php.

713 : void
714 {
715 foreach ($this->blocklist as $name => $content) {
716 preg_match_all($this->variablesRegExp, $content, $regs);
717
718 if (count($regs[1]) !== 0) {
719 foreach ($regs[1] as $var) {
720 $this->blockvariables[$name][$var] = true;
721 }
722 } else {
723 $this->blockvariables[$name] = [];
724 }
725 }
726 }

Referenced by ilTemplate\init(), init(), HTML_Template_ITX\init(), and HTML_Template_ITX\replaceBlock().

+ Here is the caller graph for this function:

◆ errorMessage()

HTML_Template_IT::errorMessage ( int  $value,
string  $blockname = '' 
)

Return a textual error message for a IT error code.

Definition at line 833 of file IT.php.

833 : string
834 {
835 static $errorMessages;
836 if (!isset($errorMessages)) {
837 $errorMessages = [
838 self::IT_OK => '',
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 ' .
846 'may appear.',
847 self::IT_UNKNOWN_OPTION => 'Unknown option'
848 ];
849 }
850
851 return $errorMessages[$value] ?? $errorMessages[self::IT_ERROR];
852 }
const IT_ERROR
Definition: IT.php:121

Referenced by get(), parse(), setCurrentBlock(), setOption(), and touchBlock().

+ Here is the caller graph for this function:

◆ findBlocks()

HTML_Template_IT::findBlocks ( string  $string)

Recusively builds a list of all blocks within the template.

Exceptions
ilTemplateException

Definition at line 732 of file IT.php.

732 : array
733 {
734 $blocklist = [];
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];
739
740 if (isset($this->blocklist[$blockname])) {
741 throw new ilTemplateException($this->errorMessage(self::IT_BLOCK_DUPLICATE, $blockname));
742 }
743
744 $this->blocklist[$blockname] = $blockcontent;
745 $this->blockdata[$blockname] = "";
746
747 $blocklist[] = $blockname;
748
749 $inner = $this->findBlocks($blockcontent);
750 foreach ($inner as $name) {
751 $pattern = sprintf(
752 '@<!--\s+BEGIN\s+%s\s+-->(.*)<!--\s+END\s+%s\s+-->@sm',
753 $name,
754 $name
755 );
756
757 $this->blocklist[$blockname] = preg_replace(
758 $pattern,
759 $this->openingDelimiter .
760 '__' . $name . '__' .
761 $this->closingDelimiter,
762 $this->blocklist[$blockname]
763 );
764 $this->blockinner[$blockname][] = $name;
765 $this->blockparents[$name] = $blockname;
766 }
767 }
768 }
769
770 return $blocklist;
771 }
findBlocks(string $string)
Recusively builds a list of all blocks within the template.
Definition: IT.php:732
array $blocklist
Array of all blocks and their content.
Definition: IT.php:204
errorMessage(int $value, string $blockname='')
Return a textual error message for a IT error code.
Definition: IT.php:833

Referenced by HTML_Template_ITX\addBlock(), ilTemplate\init(), init(), HTML_Template_ITX\init(), and HTML_Template_ITX\replaceBlock().

+ Here is the caller graph for this function:

◆ free()

HTML_Template_IT::free ( )

Clears all datafields of the object.

Don't use this function unless you know what you're doing.

Definition at line 623 of file IT.php.

623 : void
624 {
625 $this->err = [];
626
627 $this->currentBlock = self::IT_DEFAULT_BLOCK;
628
629 $this->variableCache = [];
630 $this->blocklist = [];
631 $this->touchedBlocks = [];
632
633 $this->flagBlocktrouble = false;
634 $this->flagGlobalParsed = false;
635 }
const IT_DEFAULT_BLOCK
Definition: IT.php:126

References IT_DEFAULT_BLOCK.

Referenced by ilTemplate\init(), init(), and HTML_Template_ITX\init().

+ Here is the caller graph for this function:

◆ get()

HTML_Template_IT::get ( string  $block = self::IT_DEFAULT_BLOCK)

Returns a block with all replacements done.

Exceptions
ilTemplateException

Reimplemented in ilTemplate.

Definition at line 388 of file IT.php.

388 : string
389 {
390 if ($block === self::IT_DEFAULT_BLOCK && !$this->flagGlobalParsed) {
391 $this->parse();
392 }
393
394 if (!isset($this->blocklist[$block])) {
395 throw new ilTemplateException($this->errorMessage(self::IT_BLOCK_NOT_FOUND) . '"' . $block . "'");
396 }
397
398 if (isset($this->blockdata[$block])) {
399 $ret = $this->blockdata[$block];
400 if ($this->clearCache) {
401 unset($this->blockdata[$block]);
402 }
403 if ($this->_options['preserve_data']) {
404 $ret = str_replace(
405 $this->openingDelimiter .
406 '%preserved%' . $this->closingDelimiter,
407 $this->openingDelimiter,
408 $ret
409 );
410 }
411 return $ret;
412 }
413
414 return '';
415 }
parse(string $block=self::IT_DEFAULT_BLOCK, bool $flag_recursion=false)
Parses the given block.
Definition: IT.php:424

References errorMessage(), and parse().

+ Here is the call graph for this function:

◆ getFile()

HTML_Template_IT::getFile ( string  $filename)

Reads a file from disk and returns its content.

Exceptions
ilTemplateException

Reimplemented in ilIndependantTemplate.

Definition at line 777 of file IT.php.

777 : string
778 {
779 if ($filename[0] === '/' && substr($this->fileRoot, -1) === '/') {
780 $filename = substr($filename, 1);
781 }
782
783 $filename = $this->fileRoot . $filename;
784
785 $this->real_filename = $filename;
786
787 if (($content = $this->template_cache->get($filename, new StringTransformation())) === null) {
788 if (!($fh = @fopen($filename, 'rb'))) {
789 throw new ilTemplateException($this->errorMessage(self::IT_TPL_NOT_FOUND) . ': "' . $filename . '"');
790 }
791
792 $fsize = filesize($filename);
793 if ($fsize < 1) {
794 fclose($fh);
795 return '';
796 }
797
798 $content = fread($fh, $fsize);
799 $this->template_cache->set($filename, $content);
800 fclose($fh);
801 }
802
803 return $content;
804 }
$filename
Definition: buildRTE.php:78

References $filename.

Referenced by HTML_Template_ITX\addBlockfile(), ilTemplate\addBlockFile(), and ilTemplate\loadTemplatefile().

+ Here is the caller graph for this function:

◆ init()

HTML_Template_IT::init ( )
protected

Clears all datafields of the object and rebuild the internal blocklist LoadTemplatefile() and setTemplate() automatically call this function when a new template is given.

Don't use this function unless you know what you're doing.

Exceptions
ilTemplateException

Reimplemented in ilTemplate, and HTML_Template_ITX.

Definition at line 594 of file IT.php.

594 : void
595 {
596 $this->free();
597
598 if (($blockdata = $this->block_cache->get($this->real_filename, new ListTransformation(new StringTransformation()))) !== null) {
599 $this->blockdata = $blockdata['blockdata'];
600 $this->blocklist = $blockdata['blocklist'];
601 } else {
602 $this->findBlocks($this->template);
603 $blockdata['blockdata'] = $this->blockdata;
604 $blockdata['blocklist'] = $this->blocklist;
605 $this->block_cache->set($this->real_filename, $blockdata);
606 }
607
608 // we don't need it any more
609 $this->template = '';
610
611 if (($blockvariables = $this->variable_cache->get($this->real_filename))!==null) {
612 $this->blockvariables = $blockvariables;
613 } else {
614 $this->buildBlockvariablelist();
615 $this->variable_cache->set($this->real_filename, $this->blockvariables);
616 }
617 }
free()
Clears all datafields of the object.
Definition: IT.php:623
buildBlockvariablelist()
Build a list of all variables within of a block.
Definition: IT.php:713
array $blockdata
Array with the parsed content of a block.
Definition: IT.php:209
array $blockvariables
Array of variables in a block.
Definition: IT.php:214

References $blockdata, $blocklist, $blockvariables, buildBlockvariablelist(), findBlocks(), and free().

+ Here is the call graph for this function:

◆ loadTemplatefile()

HTML_Template_IT::loadTemplatefile ( string  $filename,
bool  $removeUnknownVariables = true,
bool  $removeEmptyBlocks = true 
)

Reads a template file from the disk.

Exceptions
ilTemplateException

Reimplemented in ilIndependantTemplate, and ilTemplate.

Definition at line 675 of file IT.php.

679 : bool {
680 $template = '';
681 if (!$this->flagCacheTemplatefile ||
682 $this->lastTemplatefile !== $filename
683 ) {
684 $template = $this->getFile($filename);
685 }
686 $this->lastTemplatefile = $filename;
687
688 return $template !== '' && $this->setTemplate(
689 $template,
692 );
693 }
bool $removeEmptyBlocks
Controls the handling of empty blocks, default is remove.
Definition: IT.php:184
string $template
Content of the template.
Definition: IT.php:199
getFile(string $filename)
Reads a file from disk and returns its content.
Definition: IT.php:777
setTemplate(string $template, bool $removeUnknownVariables=true, bool $removeEmptyBlocks=true)
Sets the template.
Definition: IT.php:643
bool $removeUnknownVariables
Controls the handling of unknown variables, default is remove.
Definition: IT.php:179

References $filename.

◆ parse()

HTML_Template_IT::parse ( string  $block = self::IT_DEFAULT_BLOCK,
bool  $flag_recursion = false 
)

Parses the given block.

Parameters
stringname of the block to be parsed @access public
Exceptions
ilTemplateException
See also
parseCurrentBlock()

Definition at line 424 of file IT.php.

424 : bool
425 {
426 static $regs, $values;
427
428 if (!isset($this->blocklist[$block])) {
429 throw new ilTemplateException($this->errorMessage(self::IT_BLOCK_NOT_FOUND) . '"' . $block . "'");
430 }
431
432 if (self::IT_DEFAULT_BLOCK === $block) {
433 $this->flagGlobalParsed = true;
434 }
435
436 if (!$flag_recursion) {
437 $regs = [];
438 $values = [];
439 }
440 $outer = $this->blocklist[$block];
441 $empty = true;
442
443 if ($this->clearCacheOnParse) {
444 foreach ($this->variableCache as $name => $value) {
445 $regs[] = $this->openingDelimiter .
447 $values[] = $value;
448 $empty = false;
449 }
450 $this->variableCache = [];
451 } else {
452 foreach ($this->blockvariables[$block] as $allowedvar => $v) {
453 if (isset($this->variableCache[$allowedvar])) {
454 $regs[] = $this->openingDelimiter .
455 $allowedvar . $this->closingDelimiter;
456 $values[] = $this->variableCache[$allowedvar];
457 unset($this->variableCache[$allowedvar]);
458 $empty = false;
459 }
460 }
461 }
462
463 if (isset($this->blockinner[$block])) {
464 foreach ($this->blockinner[$block] as $k => $innerblock) {
465 $this->parse($innerblock, true);
466 if ($this->blockdata[$innerblock] !== '') {
467 $empty = false;
468 }
469
470 $placeholder = $this->openingDelimiter . "__" .
471 $innerblock . "__" . $this->closingDelimiter;
472 $outer = str_replace(
473 $placeholder,
474 $this->blockdata[$innerblock],
475 $outer
476 );
477 $this->blockdata[$innerblock] = "";
478 }
479 }
480
481 if (!$flag_recursion && 0 !== count($values)) {
482 if ($this->_options['use_preg']) {
483 $regs = array_map(
484 [
485 &$this,
486 '_addPregDelimiters'
487 ],
488 $regs
489 );
490 $funcReplace = 'preg_replace';
491 } else {
492 $funcReplace = 'str_replace';
493 }
494
495 if ($this->_options['preserve_data']) {
496 $values = array_map(
497 [&$this, '_preserveOpeningDelimiter'],
498 $values
499 );
500 }
501
502 $outer = $funcReplace($regs, $values, $outer);
503
504 if ($this->removeUnknownVariables) {
505 $outer = preg_replace($this->removeVariablesRegExp, "", $outer);
506 }
507 }
508
509 if ($empty) {
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]);
515 }
516 } elseif (empty($this->blockdata[$block])) {
517 $this->blockdata[$block] = $outer;
518 } else {
519 $this->blockdata[$block] .= $outer;
520 }
521
522 return $empty;
523 }
string $closingDelimiter
Last character of a variable placeholder ( {VARIABLE_}_ ).
Definition: IT.php:149

References $closingDelimiter, errorMessage(), and parse().

Referenced by get(), parse(), and parseCurrentBlock().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parseCurrentBlock()

HTML_Template_IT::parseCurrentBlock ( )

Parses the current block.

Exceptions
ilTemplateException

Definition at line 529 of file IT.php.

529 : bool
530 {
531 return $this->parse($this->currentBlock);
532 }

References parse().

Referenced by ilTemplate\touchBlock().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setCurrentBlock()

HTML_Template_IT::setCurrentBlock ( string  $block = self::IT_DEFAULT_BLOCK)

Sets the name of the current block that is the block where variables are added.

Exceptions
ilTemplateException

Reimplemented in ilTemplate.

Definition at line 561 of file IT.php.

561 : bool
562 {
563 if (!isset($this->blocklist[$block])) {
564 throw new ilTemplateException($this->errorMessage(self::IT_BLOCK_NOT_FOUND) . '"' . $block . "'");
565 }
566
567 $this->currentBlock = $block;
568
569 return true;
570 }

References errorMessage().

+ Here is the call graph for this function:

◆ setOption()

HTML_Template_IT::setOption ( string  $option,
  $value 
)

Sets the option for the template class.

Parameters
mixed$value
Exceptions
ilTemplateException

Definition at line 351 of file IT.php.

351 : int
352 {
353 if (array_key_exists($option, $this->_options)) {
354 $this->_options[$option] = $value;
355 return self::IT_OK;
356 }
357
358 throw new ilTemplateException($this->errorMessage(self::IT_UNKNOWN_OPTION) . ": '$option'");
359 }
const IT_OK
Definition: IT.php:120

References errorMessage(), and IT_OK.

Referenced by ilTemplate\__construct(), and setOptions().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setOptions()

HTML_Template_IT::setOptions ( array  $options)

Sets the options for the template class.

Parameters
string[]$options
Exceptions
ilTemplateException

Definition at line 366 of file IT.php.

366 : int
367 {
368 foreach ($options as $option => $value) {
369 $this->setOption($option, $value);
370 }
371
372 return self::IT_OK;
373 }
setOption(string $option, $value)
Sets the option for the template class.
Definition: IT.php:351

References IT_OK, and setOption().

Referenced by __construct().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setRoot()

HTML_Template_IT::setRoot ( string  $root)

Sets the file root.

The file root gets prefixed to all filenames passed to the object. Make sure that you override this function when using the class on windows.

Definition at line 701 of file IT.php.

701 : void
702 {
703 if ($root !== '' && substr($root, -1) !== '/') {
704 $root .= '/';
705 }
706
707 $this->fileRoot = $root;
708 }

Referenced by __construct().

+ Here is the caller graph for this function:

◆ setTemplate()

HTML_Template_IT::setTemplate ( string  $template,
bool  $removeUnknownVariables = true,
bool  $removeEmptyBlocks = true 
)

Sets the template.

You can eighter load a template file from disk with LoadTemplatefile() or set the template manually using this function.

Exceptions
ilTemplateException

Definition at line 643 of file IT.php.

647 : bool {
648 $this->removeUnknownVariables = $removeUnknownVariables;
649 $this->removeEmptyBlocks = $removeEmptyBlocks;
650
651 if ($template === '' && $this->flagCacheTemplatefile) {
652 $this->variableCache = [];
653 $this->blockdata = [];
654 $this->touchedBlocks = [];
655 $this->currentBlock = self::IT_DEFAULT_BLOCK;
656 } else {
657 $this->template =
658 '<!-- BEGIN ' . self::IT_DEFAULT_BLOCK . ' -->' .
659 $template .
660 '<!-- END ' . self::IT_DEFAULT_BLOCK . ' -->';
661 $this->init();
662 }
663
664 if ($this->flagBlocktrouble) {
665 return false;
666 }
667
668 return true;
669 }
init()
Clears all datafields of the object and rebuild the internal blocklist LoadTemplatefile() and setTemp...
Definition: IT.php:594

References IT_DEFAULT_BLOCK.

◆ setVariable()

HTML_Template_IT::setVariable (   $variable,
  $value = '' 
)

Sets a variable value.

The function can be used eighter like setVariable( "varname", "value") or with one array $variables["varname"] = "value" given setVariable($variables) quite like phplib templates set_var().

Parameters
string | array$variablestring with the variable name or an array variables["varname"] = "value"
mixed$valuevalue of the variable or empty if $variable is an array.

Definition at line 544 of file IT.php.

544 : void
545 {
546 if (is_array($variable)) {
547 $this->variableCache = array_merge(
548 $this->variableCache,
549 $variable
550 );
551 } else {
552 $this->variableCache[$variable] = $value;
553 }
554 }

Referenced by ilContainerGUI\addHeaderRow(), ilMiniCalendarGUI\addMiniMonth(), ilCalendarBlockGUI\addMiniMonth(), ILIAS\Test\Questions\Presentation\Printer\addQuestionResultForTestUsersToTemplate(), ilContainerRenderer\addStandardRow(), ilCalendarBlockGUI\addSubscriptionButton(), ilTimingsCronReminder\buildMailSalutation(), ilRecurrenceInputGUI\buildMonthlyByDaySelection(), ilRecurrenceInputGUI\buildMonthlyByMonthDaySelection(), ilRecurrenceInputGUI\buildUntilSelection(), ilRecurrenceInputGUI\buildWeekDaySelection(), ilRecurrenceInputGUI\buildYearlyByDaySelection(), ilRecurrenceInputGUI\buildYearlyByMonthDaySelection(), ilObjForumGUI\createThreadObject(), ilTimingsCronReminder\fillObjectListForMailBody(), ilStudyProgrammeExpandableProgressListGUI\fillTemplate(), ilStudyProgrammeProgressListGUI\fillTemplate(), ilTemplate\fillVars(), ilSearchRootSelector\formatHeader(), ilECSNodeMappingCmsExplorer\formatHeader(), ilECSNodeMappingLocalExplorer\formatHeader(), ilPasteIntoMultipleItemsExplorer\formatHeader(), ilPasteIntoMultipleItemsExplorer\formatObject(), ilCalendarViewGUI\getContentByPlugins(), ilAbstractLearningHistoryProvider\getEmphasizedTitle(), ilCalendarSelectionBlockGUI\getLegacyContent(), ilRepositoryObjectSearchBlockGUI\getLegacyContent(), ilWikiFunctionsBlockGUI\getLegacyContent(), ilSCORMExplorer\getOutputIcons(), assNumericGUI\getPreview(), ilObjSurveyGUI\getUserResultsTable(), ilOrgUnitGenericMultiInputGUI\insert(), ilScheduleInputGUI\insert(), ilRecurrenceInputGUI\insert(), ilChatroomAuthInputGUI\insert(), ilDclGenericMultiInputGUI\insert(), ilCheckboxGroupInputGUI\insert(), ilCheckboxInputGUI\insert(), ilColorPickerInputGUI\insert(), ilCombinationInputGUI\insert(), ilCSSRectInputGUI\insert(), ilCustomInputGUI\insert(), ilDateDurationInputGUI\insert(), ilDateTimeInputGUI\insert(), ilDurationInputGUI\insert(), ilEMailInputGUI\insert(), ilFileInputGUI\insert(), ilFileWizardInputGUI\insert(), ilFormSectionHeaderGUI\insert(), ilHiddenInputGUI\insert(), ilImageFileInputGUI\insert(), ilLinkInputGUI\insert(), ilLocationInputGUI\insert(), ilMultiSelectInputGUI\insert(), ilNestedListInputGUI\insert(), ilNonEditableValueGUI\insert(), ilNumberInputGUI\insert(), ilPasswordInputGUI\insert(), ilRadioGroupInputGUI\insert(), ilRepositorySelectorInputGUI\insert(), ilSelectBuilderInputGUI\insert(), ilSelectInputGUI\insert(), ilTextAreaInputGUI\insert(), ilTextInputGUI\insert(), ilTextWizardInputGUI\insert(), ilUserLoginInputGUI\insert(), ilMailFormAttachmentPropertyGUI\insert(), ilManualPlaceholderInputGUI\insert(), ilWidthHeightInputGUI\insert(), ilOrgUnitAuthorityInputGUI\insert(), ilBackgroundImageInputGUI\insert(), ilBackgroundPositionInputGUI\insert(), ilFontSizeInputGUI\insert(), ilNumericStyleValueInputGUI\insert(), ilTRBLBorderStyleInputGUI\insert(), ilTRBLBorderWidthInputGUI\insert(), ilTRBLColorPickerInputGUI\insert(), ilTRBLNumericStyleValueInputGUI\insert(), ilMatrixRowWizardInputGUI\insert(), ilAnswerWizardInputGUI\insert(), ilErrorTextWizardInputGUI\insert(), ilEssayKeywordWizardInputGUI\insert(), ilKprimChoiceWizardInputGUI\insert(), ilMatchingPairWizardInputGUI\insert(), ilMatchingWizardInputGUI\insert(), ilMultipleChoiceWizardInputGUI\insert(), ilSingleChoiceWizardInputGUI\insert(), ilAssAnswerCorrectionsInputGUI\insert(), ilAssClozeTestCombinationVariantsInputGUI\insert(), ilAssErrorTextCorrectionsInputGUI\insert(), ilAssLongmenuCorrectionsInputGUI\insert(), ilAssMatchingPairCorrectionsInputGUI\insert(), ilAssMultipleChoiceCorrectionsInputGUI\insert(), ilAssSingleChoiceCorrectionsInputGUI\insert(), ilImagemapCorrectionsInputGUI\insert(), ilImagemapFileInputGUI\insert(), ilExplorerSelectInputGUI\insert(), ilPCParagraphGUI\insertHelp(), ilNestedList\listItemStart(), ilNestedList\listStart(), ilSurveyExecutionGUI\outNavigationButtons(), ilFileInputGUI\outputSuffixes(), SurveyQuestionGUI\outQuestionText(), ilAssQuestionPreviewGUI\populateCommentsPanel(), assMatchingQuestionGUI\populateDefinition(), ilTestServiceGUI\populateExamId(), ilAssQuestionPreviewGUI\populateGenericQuestionFeedback(), ilAssQuestionPreviewGUI\populateInstantResponseHeader(), ilAssQuestionPreviewGUI\populateInstantResponseMessage(), ilTestServiceGUI\populatePassFinishDate(), ilAssQuestionPreviewGUI\populateQuestionOutput(), ilAssQuestionPreviewGUI\populateReachedPointsOutput(), ilAssQuestionPreviewGUI\populateSolutionOutput(), ilAssQuestionPreviewGUI\populateSpecificQuestionFeedback(), ilPollAnswersRenderer\render(), ILIAS\Exercise\Assignment\PanelBuilderUI\renderActionButton(), ilPollContentRenderer\renderAlertForAnonymousUser(), ilPollContentRenderer\renderAnchor(), ilPollAnswersRenderer\renderAnswer(), ilPollContentRenderer\renderAvailability(), ilTestQuestionNavigationGUI\renderButtonInstance(), ilPollContentRenderer\renderComments(), ilAssLacLegendGUI\renderCommonLegendPart(), ilNewsTimelineGUI\renderDeleteModal(), ilPollContentRenderer\renderDescription(), ilContainerRenderer\renderDetails(), ilNewsTimelineGUI\renderEditModal(), ilAssLacLegendGUI\renderExample(), ilContainerRenderer\renderHelperGeneric(), ilCalendarSelectionBlockGUI\renderItem(), ILIAS\Chatroom\BuildChat\renderLanguageVariables(), ILIAS\Exercise\Assignment\PanelBuilderUI\renderLinkList(), ilPollContentRenderer\renderNoQuestionMessage(), ilObjForumGUI\renderPostingForm(), ilPollContentRenderer\renderQuestion(), ilAssLacLegendGUI\renderQuestSpecificExamples(), ilAssLacLegendGUI\renderQuestSpecificLegendPart(), ilChatroomViewGUI\renderRightUsersBlock(), ilContainerRenderer\renderSelectAllBlock(), ilChatroomViewGUI\renderSendMessageBox(), ilPollContentRenderer\renderTotalParticipantsInfo(), ilNewsForContextBlockGUI\showFeedUrl(), ilPDNewsBlockGUI\showFeedUrl(), ilInfoScreenGUI\showLearningProgress(), and ilNewsForContextBlockGUI\showNews().

+ Here is the caller graph for this function:

◆ show()

HTML_Template_IT::show ( string  $block = self::IT_DEFAULT_BLOCK)

Print a certain block with all replacements done.

Exceptions
ilTemplateException

Definition at line 379 of file IT.php.

379 : void
380 {
381 print $this->get($block);
382 }

◆ touchBlock()

HTML_Template_IT::touchBlock ( string  $block)

Preserves an empty block even if removeEmptyBlocks is true.

Exceptions
ilTemplateException

Reimplemented in ilTemplate.

Definition at line 576 of file IT.php.

576 : bool
577 {
578 if (!isset($this->blocklist[$block])) {
579 throw new ilTemplateException($this->errorMessage(self::IT_BLOCK_NOT_FOUND) . '"' . $block . "'");
580 }
581
582 $this->touchedBlocks[$block] = true;
583
584 return true;
585 }

References errorMessage().

+ Here is the call graph for this function:

Field Documentation

◆ $_options

array HTML_Template_IT::$_options
Initial value:
= [
'preserve_data' => false,
'use_preg' => true
]
return true

$_options['preserve_data'] Whether to substitute variables and remove empty placeholders in data passed through setVariable (see also bugs #20199, #21951).

$_options['use_preg'] Whether to use preg_replace instead of str_replace in parse() (this is a backwards compatibility feature, see also bugs #21951, #20392)

Definition at line 303 of file IT.php.

◆ $block_cache

ILIAS Cache Container Container HTML_Template_IT::$block_cache
private

Definition at line 127 of file IT.php.

◆ $blockdata

array HTML_Template_IT::$blockdata = []

Array with the parsed content of a block.

Definition at line 209 of file IT.php.

Referenced by ilTemplate\init(), init(), and HTML_Template_ITX\replaceBlock().

◆ $blockinner

array HTML_Template_IT::$blockinner = []

Array of inner blocks of a block.

Definition at line 224 of file IT.php.

Referenced by ilTemplate\init().

◆ $blocklist

array HTML_Template_IT::$blocklist = []

Array of all blocks and their content.

Definition at line 204 of file IT.php.

Referenced by ilTemplate\init(), and init().

◆ $blocknameRegExp

string HTML_Template_IT::$blocknameRegExp = '[\.0-9A-Za-z_-]+'

RegExp matching a block in the template.

Per default "sm" is used as the regexp modifier, "i" is missing. That means a case sensitive search is done.

Definition at line 156 of file IT.php.

◆ $blockparents

array HTML_Template_IT::$blockparents = []

Array of block parents.

Definition at line 219 of file IT.php.

Referenced by ilTemplate\init().

◆ $blockRegExp

string HTML_Template_IT::$blockRegExp = ''

RegExp used to find blocks an their content, filled by the constructor.

Definition at line 189 of file IT.php.

◆ $blockvariables

array HTML_Template_IT::$blockvariables = []

Array of variables in a block.

Definition at line 214 of file IT.php.

Referenced by ilTemplate\init(), and init().

◆ $clearCache

bool HTML_Template_IT::$clearCache = false

Clear cache on get()?

Definition at line 139 of file IT.php.

◆ $clearCacheOnParse

bool HTML_Template_IT::$clearCacheOnParse = false

Clear the variable cache on parse? If you're not an expert just leave the default false.

True reduces memory consumption somewhat if you tend to add lots of values for unknown placeholder.

Definition at line 261 of file IT.php.

◆ $closingDelimiter

string HTML_Template_IT::$closingDelimiter = '}'

Last character of a variable placeholder ( {VARIABLE_}_ ).

Definition at line 149 of file IT.php.

Referenced by HTML_Template_ITX\buildFunctionlist(), and parse().

◆ $currentBlock

string HTML_Template_IT::$currentBlock = self::IT_DEFAULT_BLOCK

Name of the current block.

Definition at line 194 of file IT.php.

◆ $err

array HTML_Template_IT::$err = []

Contains the error objects.

Definition at line 134 of file IT.php.

Referenced by ilTemplate\init().

◆ $fileRoot

string HTML_Template_IT::$fileRoot = ''

Root directory for all file operations.

The string gets prefixed to all filenames given.

Definition at line 267 of file IT.php.

◆ $flagBlocktrouble

bool HTML_Template_IT::$flagBlocktrouble = false

Internal flag indicating that a blockname was used multiple times.

Definition at line 272 of file IT.php.

Referenced by ilTemplate\init().

◆ $flagCacheTemplatefile

bool HTML_Template_IT::$flagCacheTemplatefile = true

EXPERIMENTAL! FIXME! Flag indication that a template gets cached.

Complex templates require some times to be preparsed before the replacement can take place. Often I use one template file over and over again but I don't know before that I will use the same template file again. Now IT could notice this and skip the preparse.

Definition at line 288 of file IT.php.

◆ $flagGlobalParsed

bool HTML_Template_IT::$flagGlobalParsed = false

Flag indicating that the global block was parsed.

Definition at line 277 of file IT.php.

◆ $lastTemplatefile

string HTML_Template_IT::$lastTemplatefile = ''

EXPERIMENTAL! FIXME!

Definition at line 293 of file IT.php.

◆ $openingDelimiter

string HTML_Template_IT::$openingDelimiter = '{'

First character of a variable placeholder ( _{_VARIABLE} ).

Definition at line 144 of file IT.php.

◆ $real_filename

string HTML_Template_IT::$real_filename = ''
protected

Holds the real template file name.

Definition at line 311 of file IT.php.

◆ $removeEmptyBlocks

bool HTML_Template_IT::$removeEmptyBlocks = true

Controls the handling of empty blocks, default is remove.

Definition at line 184 of file IT.php.

◆ $removeUnknownVariables

bool HTML_Template_IT::$removeUnknownVariables = true

Controls the handling of unknown variables, default is remove.

Definition at line 179 of file IT.php.

◆ $removeVariablesRegExp

string HTML_Template_IT::$removeVariablesRegExp = ''

RegExp used to strip unused variable placeholder.

Definition at line 174 of file IT.php.

◆ $template

string HTML_Template_IT::$template = ''

◆ $template_cache

ILIAS Cache Container Container HTML_Template_IT::$template_cache
private

Definition at line 129 of file IT.php.

◆ $touchedBlocks

array HTML_Template_IT::$touchedBlocks = []

List of blocks to preverse even if they are "empty".

This is something special. Sometimes you have blocks that should be preserved although they are empty (no placeholder replaced). Think of a shopping basket. If it's empty you have to drop a message to the user. If it's filled you have to show the contents of the shopping baseket. Now where do you place the message that the basket is empty? It's no good idea to place it in you applications as customers tend to like unecessary minor text changes. Having another template file for an empty basket means that it's very likely that one fine day the filled and empty basket templates have different layout. I decided to introduce blocks that to not contain any placeholder but only text such as the message "Your shopping basked is empty". Now if there is no replacement done in such a block the block will be recognized as "empty" and by default ($removeEmptyBlocks = true) be stripped off. To avoid thisyou can now call touchBlock() to avoid this. The array $touchedBlocks stores a list of touched block which must not be removed even if they are empty.

Definition at line 245 of file IT.php.

◆ $variable_cache

ILIAS Cache Container Container HTML_Template_IT::$variable_cache
private

Definition at line 128 of file IT.php.

◆ $variableCache

array HTML_Template_IT::$variableCache = []

Variable cache.

Variables get cached before any replacement is done. Advantage: empty blocks can be removed automatically. Disadvantage: might take some more memory

Definition at line 253 of file IT.php.

◆ $variablenameRegExp

string HTML_Template_IT::$variablenameRegExp = '[\.0-9A-Za-z_-]+'

RegExp matching a variable placeholder in the template.

Per default "sm" is used as the regexp modifier, "i" is missing. That means a case sensitive search is done.

Definition at line 163 of file IT.php.

◆ $variablesRegExp

string HTML_Template_IT::$variablesRegExp = ''

RegExp used to find variable placeholder, filled by the constructor.

Looks somewhat like @(delimiter varname delimiter).

Definition at line 169 of file IT.php.

◆ IT_BLOCK_DUPLICATE

const HTML_Template_IT::IT_BLOCK_DUPLICATE = -4

Definition at line 124 of file IT.php.

◆ IT_BLOCK_NOT_FOUND

const HTML_Template_IT::IT_BLOCK_NOT_FOUND = -3

Definition at line 123 of file IT.php.

◆ IT_DEFAULT_BLOCK

const HTML_Template_IT::IT_DEFAULT_BLOCK = '__global__'

◆ IT_ERROR

const HTML_Template_IT::IT_ERROR = -1

Definition at line 121 of file IT.php.

◆ IT_OK

const HTML_Template_IT::IT_OK = 1

Definition at line 120 of file IT.php.

Referenced by setOption(), and setOptions().

◆ IT_TPL_NOT_FOUND

const HTML_Template_IT::IT_TPL_NOT_FOUND = -2

Definition at line 122 of file IT.php.

◆ IT_UNKNOWN_OPTION

const HTML_Template_IT::IT_UNKNOWN_OPTION = -6

Definition at line 125 of file IT.php.


The documentation for this class was generated from the following file: