ILIAS  release_8 Revision v8.24
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...
 

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 97 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 298 of file IT.php.

299 {
300 if (!is_null($options)) {
301 $this->setOptions($options);
302 }
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';
309
310 $this->blockRegExp = '@<!--\s+BEGIN\s+(' . $this->blocknameRegExp .
311 ')\s+-->(.*)<!--\s+END\s+\1\s+-->@sm';
312
313 $this->setRoot($root);
314 }
setRoot(string $root)
Sets the file root.
Definition: IT.php:674
setOptions(array $options)
Sets the options for the template class.
Definition: IT.php:336

References 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 783 of file IT.php.

783 : string
784 {
785 return '@' . $str . '@';
786 }

◆ _preserveOpeningDelimiter()

HTML_Template_IT::_preserveOpeningDelimiter ( string  $str)

Replaces an opening delimiter by a special string.

Definition at line 791 of file IT.php.

791 : string
792 {
793 return (false === strpos($str, $this->openingDelimiter)) ?
794 $str :
795 str_replace(
796 $this->openingDelimiter,
797 $this->openingDelimiter .
798 '%preserved%' . $this->closingDelimiter,
799 $str
800 );
801 }

◆ buildBlockvariablelist()

HTML_Template_IT::buildBlockvariablelist ( )

Build a list of all variables within of a block.

Definition at line 686 of file IT.php.

686 : void
687 {
688 foreach ($this->blocklist as $name => $content) {
689 preg_match_all($this->variablesRegExp, $content, $regs);
690
691 if (count($regs[1]) !== 0) {
692 foreach ($regs[1] as $var) {
693 $this->blockvariables[$name][$var] = true;
694 }
695 } else {
696 $this->blockvariables[$name] = [];
697 }
698 }
699 }
if($format !==null) $name
Definition: metadata.php:247

References $name.

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 806 of file IT.php.

806 : string
807 {
808 static $errorMessages;
809 if (!isset($errorMessages)) {
810 $errorMessages = [
811 self::IT_OK => '',
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 ' .
819 'may appear.',
820 self::IT_UNKNOWN_OPTION => 'Unknown option'
821 ];
822 }
823
824 return $errorMessages[$value] ?? $errorMessages[self::IT_ERROR];
825 }
const IT_ERROR
Definition: IT.php:100

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 705 of file IT.php.

705 : array
706 {
707 $blocklist = [];
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];
712
713 if (isset($this->blocklist[$blockname])) {
714 throw new ilTemplateException($this->errorMessage(self::IT_BLOCK_DUPLICATE, $blockname));
715 }
716
717 $this->blocklist[$blockname] = $blockcontent;
718 $this->blockdata[$blockname] = "";
719
720 $blocklist[] = $blockname;
721
722 $inner = $this->findBlocks($blockcontent);
723 foreach ($inner as $name) {
724 $pattern = sprintf(
725 '@<!--\s+BEGIN\s+%s\s+-->(.*)<!--\s+END\s+%s\s+-->@sm',
726 $name,
727 $name
728 );
729
730 $this->blocklist[$blockname] = preg_replace(
731 $pattern,
732 $this->openingDelimiter .
733 '__' . $name . '__' .
734 $this->closingDelimiter,
735 $this->blocklist[$blockname]
736 );
737 $this->blockinner[$blockname][] = $name;
738 $this->blockparents[$name] = $blockname;
739 }
740 }
741 }
742
743 return $blocklist;
744 }
findBlocks(string $string)
Recusively builds a list of all blocks within the template.
Definition: IT.php:705
array $blocklist
Array of all blocks and their content.
Definition: IT.php:180
errorMessage(int $value, string $blockname='')
Return a textual error message for a IT error code.
Definition: IT.php:806

References $name.

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 596 of file IT.php.

596 : void
597 {
598 $this->err = [];
599
600 $this->currentBlock = self::IT_DEFAULT_BLOCK;
601
602 $this->variableCache = [];
603 $this->blocklist = [];
604 $this->touchedBlocks = [];
605
606 $this->flagBlocktrouble = false;
607 $this->flagGlobalParsed = false;
608 }
const IT_DEFAULT_BLOCK
Definition: IT.php:105

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, and ilIndependantTemplate.

Definition at line 358 of file IT.php.

358 : string
359 {
360 if ($block === self::IT_DEFAULT_BLOCK && !$this->flagGlobalParsed) {
361 $this->parse();
362 }
363
364 if (!isset($this->blocklist[$block])) {
365 throw new ilTemplateException($this->errorMessage(self::IT_BLOCK_NOT_FOUND) . '"' . $block . "'");
366 }
367
368 if (isset($this->blockdata[$block])) {
369 $ret = $this->blockdata[$block];
370 if ($this->clearCache) {
371 unset($this->blockdata[$block]);
372 }
373 if ($this->_options['preserve_data']) {
374 $ret = str_replace(
375 $this->openingDelimiter .
376 '%preserved%' . $this->closingDelimiter,
377 $this->openingDelimiter,
378 $ret
379 );
380 }
381 return $ret;
382 }
383
384 return '';
385 }
parse(string $block=self::IT_DEFAULT_BLOCK, bool $flag_recursion=false)
Parses the given block.
Definition: IT.php:394

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 750 of file IT.php.

750 : string
751 {
752 if ($filename[0] === '/' && substr($this->fileRoot, -1) === '/') {
753 $filename = substr($filename, 1);
754 }
755
756 $filename = $this->fileRoot . $filename;
757
758 $this->real_filename = $filename;
760 if (!$content = $ilGlobalCache->get($filename)) {
761 if (!($fh = @fopen($filename, 'rb'))) {
762 throw new ilTemplateException($this->errorMessage(self::IT_TPL_NOT_FOUND) . ': "' . $filename . '"');
763 }
764
765 $fsize = filesize($filename);
766 if ($fsize < 1) {
767 fclose($fh);
768 return '';
769 }
770
771 $content = fread($fh, $fsize);
772 $ilGlobalCache->set($filename, $content, 60);
773 fclose($fh);
774 }
775
776 return $content;
777 }
$filename
Definition: buildRTE.php:78
static getInstance(?string $component)

References $filename, ilGlobalCache\COMP_TEMPLATE, and ilGlobalCache\getInstance().

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

+ Here is the call graph for this function:
+ 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 564 of file IT.php.

564 : void
565 {
566 $this->free();
568
569 if ($blockdata = $blocks->get($this->real_filename)) {
570 $this->blockdata = $blockdata['blockdata'];
571 $this->blocklist = $blockdata['blocklist'];
572 } else {
574 $this->findBlocks($this->template);
575 $blockdata['blockdata'] = $this->blockdata;
576 $blockdata['blocklist'] = $this->blocklist;
577 $blocks->set($this->real_filename, $blockdata, 60);
578 }
579
580 // we don't need it any more
581 $this->template = '';
582
584 if ($blockvariables = $variables->get($this->real_filename)) {
585 $this->blockvariables = $blockvariables;
586 } else {
587 $this->buildBlockvariablelist();
588 $variables->set($this->real_filename, $this->blockvariables, 60);
589 }
590 }
free()
Clears all datafields of the object.
Definition: IT.php:596
buildBlockvariablelist()
Build a list of all variables within of a block.
Definition: IT.php:686
array $blockdata
Array with the parsed content of a block.
Definition: IT.php:185
array $blockvariables
Array of variables in a block.
Definition: IT.php:190
static log(string $message, int $log_level)

References $blockdata, $blocklist, $blockvariables, buildBlockvariablelist(), ilGlobalCache\COMP_TPL_BLOCKS, ilGlobalCache\COMP_TPL_VARIABLES, findBlocks(), free(), ilGlobalCache\getInstance(), ilGlobalCache\log(), and ilGlobalCacheSettings\LOG_LEVEL_FORCED.

+ 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 ilTemplate, and ilIndependantTemplate.

Definition at line 648 of file IT.php.

652 : bool {
653 $template = '';
654 if (!$this->flagCacheTemplatefile ||
655 $this->lastTemplatefile !== $filename
656 ) {
657 $template = $this->getFile($filename);
658 }
659 $this->lastTemplatefile = $filename;
660
661 return $template !== '' && $this->setTemplate(
662 $template,
665 );
666 }
bool $removeEmptyBlocks
Controls the handling of empty blocks, default is remove.
Definition: IT.php:160
string $template
Content of the template.
Definition: IT.php:175
getFile(string $filename)
Reads a file from disk and returns its content.
Definition: IT.php:750
setTemplate(string $template, bool $removeUnknownVariables=true, bool $removeEmptyBlocks=true)
Sets the template.
Definition: IT.php:616
bool $removeUnknownVariables
Controls the handling of unknown variables, default is remove.
Definition: IT.php:155

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 394 of file IT.php.

394 : bool
395 {
396 static $regs, $values;
397
398 if (!isset($this->blocklist[$block])) {
399 throw new ilTemplateException($this->errorMessage(self::IT_BLOCK_NOT_FOUND) . '"' . $block . "'");
400 }
401
402 if (self::IT_DEFAULT_BLOCK === $block) {
403 $this->flagGlobalParsed = true;
404 }
405
406 if (!$flag_recursion) {
407 $regs = [];
408 $values = [];
409 }
410 $outer = $this->blocklist[$block];
411 $empty = true;
412
413 if ($this->clearCacheOnParse) {
414 foreach ($this->variableCache as $name => $value) {
415 $regs[] = $this->openingDelimiter .
417 $values[] = $value;
418 $empty = false;
419 }
420 $this->variableCache = [];
421 } else {
422 foreach ($this->blockvariables[$block] as $allowedvar => $v) {
423 if (isset($this->variableCache[$allowedvar])) {
424 $regs[] = $this->openingDelimiter .
425 $allowedvar . $this->closingDelimiter;
426 $values[] = $this->variableCache[$allowedvar];
427 unset($this->variableCache[$allowedvar]);
428 $empty = false;
429 }
430 }
431 }
432
433 if (isset($this->blockinner[$block])) {
434 foreach ($this->blockinner[$block] as $k => $innerblock) {
435 $this->parse($innerblock, true);
436 if ($this->blockdata[$innerblock] !== '') {
437 $empty = false;
438 }
439
440 $placeholder = $this->openingDelimiter . "__" .
441 $innerblock . "__" . $this->closingDelimiter;
442 $outer = str_replace(
443 $placeholder,
444 $this->blockdata[$innerblock],
445 $outer
446 );
447 $this->blockdata[$innerblock] = "";
448 }
449 }
450
451 if (!$flag_recursion && 0 !== count($values)) {
452 if ($this->_options['use_preg']) {
453 $regs = array_map(
454 [
455 &$this,
456 '_addPregDelimiters'
457 ],
458 $regs
459 );
460 $funcReplace = 'preg_replace';
461 } else {
462 $funcReplace = 'str_replace';
463 }
464
465 if ($this->_options['preserve_data']) {
466 $values = array_map(
467 [&$this, '_preserveOpeningDelimiter'],
468 $values
469 );
470 }
471
472 $outer = $funcReplace($regs, $values, $outer);
473
474 if ($this->removeUnknownVariables) {
475 $outer = preg_replace($this->removeVariablesRegExp, "", $outer);
476 }
477 }
478
479 if ($empty) {
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]);
485 }
486 } elseif (empty($this->blockdata[$block])) {
487 $this->blockdata[$block] = $outer;
488 } else {
489 $this->blockdata[$block] .= $outer;
490 }
491
492 return $empty;
493 }
string $closingDelimiter
Last character of a variable placeholder ( {VARIABLE_}_ ).
Definition: IT.php:125

References $closingDelimiter, $name, 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 499 of file IT.php.

499 : bool
500 {
501 return $this->parse($this->currentBlock);
502 }

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 531 of file IT.php.

531 : bool
532 {
533 if (!isset($this->blocklist[$block])) {
534 throw new ilTemplateException($this->errorMessage(self::IT_BLOCK_NOT_FOUND) . '"' . $block . "'");
535 }
536
537 $this->currentBlock = $block;
538
539 return true;
540 }

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 321 of file IT.php.

321 : int
322 {
323 if (array_key_exists($option, $this->_options)) {
324 $this->_options[$option] = $value;
325 return self::IT_OK;
326 }
327
328 throw new ilTemplateException($this->errorMessage(self::IT_UNKNOWN_OPTION) . ": '$option'");
329 }
const IT_OK
Definition: IT.php:99

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 336 of file IT.php.

336 : int
337 {
338 foreach ($options as $option => $value) {
339 $this->setOption($option, $value);
340 }
341
342 return self::IT_OK;
343 }
setOption(string $option, $value)
Sets the option for the template class.
Definition: IT.php:321

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 674 of file IT.php.

674 : void
675 {
676 if ($root !== '' && substr($root, -1) !== '/') {
677 $root .= '/';
678 }
679
680 $this->fileRoot = $root;
681 }

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 616 of file IT.php.

620 : bool {
621 $this->removeUnknownVariables = $removeUnknownVariables;
622 $this->removeEmptyBlocks = $removeEmptyBlocks;
623
624 if ($template === '' && $this->flagCacheTemplatefile) {
625 $this->variableCache = [];
626 $this->blockdata = [];
627 $this->touchedBlocks = [];
628 $this->currentBlock = self::IT_DEFAULT_BLOCK;
629 } else {
630 $this->template =
631 '<!-- BEGIN ' . self::IT_DEFAULT_BLOCK . ' -->' .
632 $template .
633 '<!-- END ' . self::IT_DEFAULT_BLOCK . ' -->';
634 $this->init();
635 }
636
637 if ($this->flagBlocktrouble) {
638 return false;
639 }
640
641 return true;
642 }
init()
Clears all datafields of the object and rebuild the internal blocklist LoadTemplatefile() and setTemp...
Definition: IT.php:564

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 514 of file IT.php.

514 : void
515 {
516 if (is_array($variable)) {
517 $this->variableCache = array_merge(
518 $this->variableCache,
519 $variable
520 );
521 } else {
522 $this->variableCache[$variable] = $value;
523 }
524 }

Referenced by ilContainerGUI\addHeaderRow(), ilMiniCalendarGUI\addMiniMonth(), ilCalendarBlockGUI\addMiniMonth(), ilContainerRenderer\addStandardRow(), ilCalendarBlockGUI\addSubscriptionButton(), ilTestScoringByQuestionsGUI\appendFormToModal(), ilTestScoringByQuestionsGUI\appendQuestionTitleToModal(), ilTestScoringByQuestionsGUI\appendUserNameToModal(), ilRecurrenceInputGUI\buildMonthlyByDaySelection(), ilRecurrenceInputGUI\buildUntilSelection(), ilRecurrenceInputGUI\buildWeekDaySelection(), ilStudyProgrammeExpandableProgressListGUI\fillTemplate(), ilStudyProgrammeProgressListGUI\fillTemplate(), ilTemplate\fillVars(), ilSearchRootSelector\formatHeader(), ilECSNodeMappingCmsExplorer\formatHeader(), ilECSNodeMappingLocalExplorer\formatHeader(), ilPasteIntoMultipleItemsExplorer\formatHeader(), ilCalendarViewGUI\getContentByPlugins(), ilAbstractLearningHistoryProvider\getEmphasizedTitle(), ilAsyncPropertyFormGUI\getErrorMessageTemplate(), ilWikiFunctionsBlockGUI\getLegacyContent(), ilCalendarSelectionBlockGUI\getLegacyContent(), ilRepositoryObjectSearchBlockGUI\getLegacyContent(), ilSCORMExplorer\getOutputIcons(), assLongMenuGUI\getSolutionOutput(), ilObjSurveyGUI\getUserResultsTable(), ilOrgUnitGenericMultiInputGUI\insert(), ilScheduleInputGUI\insert(), ilChatroomAuthInputGUI\insert(), ilDclGenericMultiInputGUI\insert(), ilGloAdvColSortInputGUI\insert(), ilOrgUnitAuthorityInputGUI\insert(), ilOrgUnitMultiLineInputGUI\insert(), ilMatrixRowWizardInputGUI\insert(), ilAnswerWizardInputGUI\insert(), ilErrorTextWizardInputGUI\insert(), ilEssayKeywordWizardInputGUI\insert(), ilImageWizardInputGUI\insert(), ilKVPWizardInputGUI\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(), ilRecurrenceInputGUI\insert(), ilAdvSelectInputGUI\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(), ilTypicalLearningTimeInputGUI\insert(), ilBackgroundImageInputGUI\insert(), ilBackgroundPositionInputGUI\insert(), ilFontSizeInputGUI\insert(), ilNumericStyleValueInputGUI\insert(), ilTRBLBorderStyleInputGUI\insert(), ilTRBLBorderWidthInputGUI\insert(), ilTRBLColorPickerInputGUI\insert(), ilTRBLNumericStyleValueInputGUI\insert(), ilExplorerSelectInputGUI\insert(), ilClozeGapInputBuilderGUI\insert(), ilPCParagraphGUI\insertHelp(), ilSurveyExecutionGUI\outNavigationButtons(), ilFileInputGUI\outputSuffixes(), SurveyQuestionGUI\outQuestionText(), ilTestServiceGUI\populateExamId(), ilAssQuestionPreviewGUI\populateSolutionOutput(), ilHierarchyFormGUI\renderChild(), ilPreviewGUI\renderCommand(), ilContainerRenderer\renderDetails(), ilContainerRenderer\renderHelperGeneric(), ilCalendarSelectionBlockGUI\renderItem(), ilChatroomViewGUI\renderLanguageVariables(), ilSurveyPageEditGUI\renderPageNode(), ilAssLacLegendGUI\renderQuestSpecificLegendPart(), ilChatroomViewGUI\renderRightUsersBlock(), ilContainerRenderer\renderSelectAllBlock(), ilChatroomViewGUI\renderSendMessageBox(), ilNewsForContextBlockGUI\showFeedUrl(), ilPDNewsBlockGUI\showFeedUrl(), ilInfoScreenGUI\showLearningProgress(), ilContainerSessionsContentGUI\showMaterials(), 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 349 of file IT.php.

349 : void
350 {
351 print $this->get($block);
352 }

◆ touchBlock()

HTML_Template_IT::touchBlock ( string  $block)

Preserves an empty block even if removeEmptyBlocks is true.

Exceptions
ilTemplateException

Reimplemented in ilTemplate.

Definition at line 546 of file IT.php.

546 : bool
547 {
548 if (!isset($this->blocklist[$block])) {
549 throw new ilTemplateException($this->errorMessage(self::IT_BLOCK_NOT_FOUND) . '"' . $block . "'");
550 }
551
552 $this->touchedBlocks[$block] = true;
553
554 return true;
555 }

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 279 of file IT.php.

◆ $blockdata

array HTML_Template_IT::$blockdata = []

Array with the parsed content of a block.

Definition at line 185 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 200 of file IT.php.

Referenced by ilTemplate\init().

◆ $blocklist

array HTML_Template_IT::$blocklist = []

Array of all blocks and their content.

Definition at line 180 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 132 of file IT.php.

◆ $blockparents

array HTML_Template_IT::$blockparents = []

Array of block parents.

Definition at line 195 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 165 of file IT.php.

◆ $blockvariables

array HTML_Template_IT::$blockvariables = []

Array of variables in a block.

Definition at line 190 of file IT.php.

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

◆ $clearCache

bool HTML_Template_IT::$clearCache = false

Clear cache on get()?

Definition at line 115 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 237 of file IT.php.

◆ $closingDelimiter

string HTML_Template_IT::$closingDelimiter = '}'

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

Definition at line 125 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 170 of file IT.php.

◆ $err

array HTML_Template_IT::$err = []

Contains the error objects.

Definition at line 110 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 243 of file IT.php.

◆ $flagBlocktrouble

bool HTML_Template_IT::$flagBlocktrouble = false

Internal flag indicating that a blockname was used multiple times.

Definition at line 248 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 264 of file IT.php.

◆ $flagGlobalParsed

bool HTML_Template_IT::$flagGlobalParsed = false

Flag indicating that the global block was parsed.

Definition at line 253 of file IT.php.

◆ $lastTemplatefile

string HTML_Template_IT::$lastTemplatefile = ''

EXPERIMENTAL! FIXME!

Definition at line 269 of file IT.php.

◆ $openingDelimiter

string HTML_Template_IT::$openingDelimiter = '{'

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

Definition at line 120 of file IT.php.

◆ $real_filename

string HTML_Template_IT::$real_filename = ''
protected

Holds the real template file name.

Definition at line 287 of file IT.php.

◆ $removeEmptyBlocks

bool HTML_Template_IT::$removeEmptyBlocks = true

Controls the handling of empty blocks, default is remove.

Definition at line 160 of file IT.php.

◆ $removeUnknownVariables

bool HTML_Template_IT::$removeUnknownVariables = true

Controls the handling of unknown variables, default is remove.

Definition at line 155 of file IT.php.

◆ $removeVariablesRegExp

string HTML_Template_IT::$removeVariablesRegExp = ''

RegExp used to strip unused variable placeholder.

Definition at line 150 of file IT.php.

◆ $template

string HTML_Template_IT::$template = ''

◆ $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 221 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 229 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 139 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 145 of file IT.php.

◆ IT_BLOCK_DUPLICATE

const HTML_Template_IT::IT_BLOCK_DUPLICATE = -4

Definition at line 103 of file IT.php.

◆ IT_BLOCK_NOT_FOUND

const HTML_Template_IT::IT_BLOCK_NOT_FOUND = -3

Definition at line 102 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 100 of file IT.php.

◆ IT_OK

const HTML_Template_IT::IT_OK = 1

Definition at line 99 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 101 of file IT.php.

◆ IT_UNKNOWN_OPTION

const HTML_Template_IT::IT_UNKNOWN_OPTION = -6

Definition at line 104 of file IT.php.


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