ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
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

array 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 120 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[]$options array of options.
Exceptions
ilTemplateException

Definition at line 324 of file IT.php.

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

325  {
326  global $DIC;
327  $DIC = $DIC instanceof Container ? $DIC : new Container([]);
328  $this->block_cache = $DIC->globalCache()->get(new BlockCache());
329  $this->variable_cache = $DIC->globalCache()->get(new VariableCache());
330  $this->template_cache = $DIC->globalCache()->get(new TemplateCache());
331 
332  if (!is_null($options)) {
333  $this->setOptions($options);
334  }
335  $this->variablesRegExp = '@' . $this->openingDelimiter .
336  '(' . $this->variablenameRegExp . ')' .
337  $this->closingDelimiter . '@sm';
338  $this->removeVariablesRegExp = '@' . $this->openingDelimiter .
339  "\s*(" . $this->variablenameRegExp .
340  ")\s*" . $this->closingDelimiter . '@sm';
341 
342  $this->blockRegExp = '@<!--\s+BEGIN\s+(' . $this->blocknameRegExp .
343  ')\s+-->(.*)<!--\s+END\s+\1\s+-->@sm';
344 
345  $this->setRoot($root);
346  }
setOptions(array $options)
Sets the options for the template class.
Definition: IT.php:368
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:35
global $DIC
Definition: shib_login.php:25
setRoot(string $root)
Sets the file root.
Definition: IT.php:703
+ 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 812 of file IT.php.

812  : string
813  {
814  return '@' . $str . '@';
815  }

◆ _preserveOpeningDelimiter()

HTML_Template_IT::_preserveOpeningDelimiter ( string  $str)

Replaces an opening delimiter by a special string.

Definition at line 820 of file IT.php.

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

◆ buildBlockvariablelist()

HTML_Template_IT::buildBlockvariablelist ( )

Build a list of all variables within of a block.

Definition at line 715 of file IT.php.

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

715  : void
716  {
717  foreach ($this->blocklist as $name => $content) {
718  preg_match_all($this->variablesRegExp, $content, $regs);
719 
720  if (count($regs[1]) !== 0) {
721  foreach ($regs[1] as $var) {
722  $this->blockvariables[$name][$var] = true;
723  }
724  } else {
725  $this->blockvariables[$name] = [];
726  }
727  }
728  }
+ 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 835 of file IT.php.

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

835  : string
836  {
837  static $errorMessages;
838  if (!isset($errorMessages)) {
839  $errorMessages = [
840  self::IT_OK => '',
841  self::IT_ERROR => 'unknown error',
842  self::IT_TPL_NOT_FOUND => 'Cannot read the template file',
843  self::IT_BLOCK_NOT_FOUND => 'Cannot find this block',
844  self::IT_BLOCK_DUPLICATE => 'The name of a block must be' .
845  ' uniquewithin a template.' .
846  ' Found "' . $blockname . '" twice.' .
847  'Unpredictable results ' .
848  'may appear.',
849  self::IT_UNKNOWN_OPTION => 'Unknown option'
850  ];
851  }
852 
853  return $errorMessages[$value] ?? $errorMessages[self::IT_ERROR];
854  }
+ 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 734 of file IT.php.

References $blocklist, and errorMessage().

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

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

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

625  : void
626  {
627  $this->err = [];
628 
629  $this->currentBlock = self::IT_DEFAULT_BLOCK;
630 
631  $this->variableCache = [];
632  $this->blocklist = [];
633  $this->touchedBlocks = [];
634 
635  $this->flagBlocktrouble = false;
636  $this->flagGlobalParsed = false;
637  }
+ 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

Definition at line 390 of file IT.php.

References errorMessage(), and parse().

390  : string
391  {
392  if ($block === self::IT_DEFAULT_BLOCK && !$this->flagGlobalParsed) {
393  $this->parse();
394  }
395 
396  if (!isset($this->blocklist[$block])) {
397  throw new ilTemplateException($this->errorMessage(self::IT_BLOCK_NOT_FOUND) . '"' . $block . "'");
398  }
399 
400  if (isset($this->blockdata[$block])) {
401  $ret = $this->blockdata[$block];
402  if ($this->clearCache) {
403  unset($this->blockdata[$block]);
404  }
405  if ($this->_options['preserve_data']) {
406  $ret = str_replace(
407  $this->openingDelimiter .
408  '%preserved%' . $this->closingDelimiter,
409  $this->openingDelimiter,
410  $ret
411  );
412  }
413  return $ret;
414  }
415 
416  return '';
417  }
errorMessage(int $value, string $blockname='')
Return a textual error message for a IT error code.
Definition: IT.php:835
parse(string $block=self::IT_DEFAULT_BLOCK, bool $flag_recursion=false)
Parses the given block.
Definition: IT.php:426
+ 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

Definition at line 779 of file IT.php.

References $filename, and errorMessage().

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

779  : string
780  {
781  if ($filename[0] === '/' && substr($this->fileRoot, -1) === '/') {
782  $filename = substr($filename, 1);
783  }
784 
785  $filename = $this->fileRoot . $filename;
786 
787  $this->real_filename = $filename;
788 
789  if (($content = $this->template_cache->get($filename, new StringTransformation())) === null) {
790  if (!($fh = @fopen($filename, 'rb'))) {
791  throw new ilTemplateException($this->errorMessage(self::IT_TPL_NOT_FOUND) . ': "' . $filename . '"');
792  }
793 
794  $fsize = filesize($filename);
795  if ($fsize < 1) {
796  fclose($fh);
797  return '';
798  }
799 
800  $content = fread($fh, $fsize);
801  $this->template_cache->set($filename, $content);
802  fclose($fh);
803  }
804 
805  return $content;
806  }
errorMessage(int $value, string $blockname='')
Return a textual error message for a IT error code.
Definition: IT.php:835
$filename
Definition: buildRTE.php:78
+ 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

Definition at line 596 of file IT.php.

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

Referenced by setTemplate().

596  : void
597  {
598  $this->free();
599 
600  if (($blockdata = $this->block_cache->get($this->real_filename, new ListTransformation(new StringTransformation()))) !== null) {
601  $this->blockdata = $blockdata['blockdata'];
602  $this->blocklist = $blockdata['blocklist'];
603  } else {
604  $this->findBlocks($this->template);
605  $blockdata['blockdata'] = $this->blockdata;
606  $blockdata['blocklist'] = $this->blocklist;
607  $this->block_cache->set($this->real_filename, $blockdata);
608  }
609 
610  // we don't need it any more
611  $this->template = '';
612 
613  if (($blockvariables = $this->variable_cache->get($this->real_filename))!==null) {
614  $this->blockvariables = $blockvariables;
615  } else {
616  $this->buildBlockvariablelist();
617  $this->variable_cache->set($this->real_filename, $this->blockvariables);
618  }
619  }
array $blockvariables
Array of variables in a block.
Definition: IT.php:216
findBlocks(string $string)
Recusively builds a list of all blocks within the template.
Definition: IT.php:734
free()
Clears all datafields of the object.
Definition: IT.php:625
buildBlockvariablelist()
Build a list of all variables within of a block.
Definition: IT.php:715
array $blockdata
Array with the parsed content of a block.
Definition: IT.php:211
array $blocklist
Array of all blocks and their content.
Definition: IT.php:206
+ Here is the call graph for this function:
+ Here is the caller 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

Definition at line 677 of file IT.php.

References $filename, getFile(), and setTemplate().

Referenced by ilIndependantTemplate\loadTemplatefile().

681  : bool {
682  $template = '';
683  if (!$this->flagCacheTemplatefile ||
684  $this->lastTemplatefile !== $filename
685  ) {
686  $template = $this->getFile($filename);
687  }
688  $this->lastTemplatefile = $filename;
689 
690  return $template !== '' && $this->setTemplate(
691  $template,
694  );
695  }
setTemplate(string $template, bool $removeUnknownVariables=true, bool $removeEmptyBlocks=true)
Sets the template.
Definition: IT.php:645
bool $removeEmptyBlocks
Controls the handling of empty blocks, default is remove.
Definition: IT.php:186
string $template
Content of the template.
Definition: IT.php:201
bool $removeUnknownVariables
Controls the handling of unknown variables, default is remove.
Definition: IT.php:181
getFile(string $filename)
Reads a file from disk and returns its content.
Definition: IT.php:779
$filename
Definition: buildRTE.php:78
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ 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 public
Exceptions
ilTemplateException
See also
parseCurrentBlock()

Definition at line 426 of file IT.php.

References $closingDelimiter, and errorMessage().

Referenced by get(), and parseCurrentBlock().

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

References parse().

Referenced by ilTemplate\touchBlock().

531  : bool
532  {
533  return $this->parse($this->currentBlock);
534  }
parse(string $block=self::IT_DEFAULT_BLOCK, bool $flag_recursion=false)
Parses the given block.
Definition: IT.php:426
+ 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

Definition at line 563 of file IT.php.

References errorMessage().

563  : bool
564  {
565  if (!isset($this->blocklist[$block])) {
566  throw new ilTemplateException($this->errorMessage(self::IT_BLOCK_NOT_FOUND) . '"' . $block . "'");
567  }
568 
569  $this->currentBlock = $block;
570 
571  return true;
572  }
errorMessage(int $value, string $blockname='')
Return a textual error message for a IT error code.
Definition: IT.php:835
+ 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 353 of file IT.php.

References errorMessage().

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

353  : int
354  {
355  if (array_key_exists($option, $this->_options)) {
356  $this->_options[$option] = $value;
357  return self::IT_OK;
358  }
359 
360  throw new ilTemplateException($this->errorMessage(self::IT_UNKNOWN_OPTION) . ": '$option'");
361  }
errorMessage(int $value, string $blockname='')
Return a textual error message for a IT error code.
Definition: IT.php:835
+ 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 368 of file IT.php.

References setOption().

Referenced by __construct().

368  : int
369  {
370  foreach ($options as $option => $value) {
371  $this->setOption($option, $value);
372  }
373 
374  return self::IT_OK;
375  }
setOption(string $option, $value)
Sets the option for the template class.
Definition: IT.php:353
+ 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 703 of file IT.php.

Referenced by __construct().

703  : void
704  {
705  if ($root !== '' && substr($root, -1) !== '/') {
706  $root .= '/';
707  }
708 
709  $this->fileRoot = $root;
710  }
+ 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 645 of file IT.php.

References $removeEmptyBlocks, $removeUnknownVariables, and init().

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

649  : bool {
650  $this->removeUnknownVariables = $removeUnknownVariables;
651  $this->removeEmptyBlocks = $removeEmptyBlocks;
652 
653  if ($template === '' && $this->flagCacheTemplatefile) {
654  $this->variableCache = [];
655  $this->blockdata = [];
656  $this->touchedBlocks = [];
657  $this->currentBlock = self::IT_DEFAULT_BLOCK;
658  } else {
659  $this->template =
660  '<!-- BEGIN ' . self::IT_DEFAULT_BLOCK . ' -->' .
661  $template .
662  '<!-- END ' . self::IT_DEFAULT_BLOCK . ' -->';
663  $this->init();
664  }
665 
666  if ($this->flagBlocktrouble) {
667  return false;
668  }
669 
670  return true;
671  }
bool $removeEmptyBlocks
Controls the handling of empty blocks, default is remove.
Definition: IT.php:186
string $template
Content of the template.
Definition: IT.php:201
bool $removeUnknownVariables
Controls the handling of unknown variables, default is remove.
Definition: IT.php:181
init()
Clears all datafields of the object and rebuild the internal blocklist LoadTemplatefile() and setTemp...
Definition: IT.php:596
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

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

Referenced by ilContainerRenderer\addHeaderRow(), ilContainerGUI\addHeaderRow(), ilContainerGUI\addMessageRow(), ilMiniCalendarGUI\addMiniMonth(), ilCalendarBlockGUI\addMiniMonth(), ILIAS\Test\Questions\Presentation\Printer\addQuestionResultForTestUsersToTemplate(), ILIAS\Test\Questions\Presentation\Printer\addResultUserInfoToTemplate(), ilExerciseGSToolProvider\addSection(), ilContainerRenderer\addStandardRow(), ilCalendarBlockGUI\addSubscriptionButton(), ILIAS\Survey\Mode\AbstractUIModifier\buildExportButtonAndModal(), ilTimingsCronReminder\buildMailSalutation(), ilRecurrenceInputGUI\buildMonthlyByDaySelection(), ilRecurrenceInputGUI\buildMonthlyByMonthDaySelection(), ilRecurrenceInputGUI\buildUntilSelection(), ilRecurrenceInputGUI\buildWeekDaySelection(), ilRecurrenceInputGUI\buildYearlyByDaySelection(), ilRecurrenceInputGUI\buildYearlyByMonthDaySelection(), ilKprimChoiceCorrectionsInputGUI\checkInput(), ilTimingsCronReminder\fillObjectListForMailBody(), ilStudyProgrammeProgressListGUI\fillTemplate(), ilStudyProgrammeExpandableProgressListGUI\fillTemplate(), ilTemplate\fillVars(), ilSCORMExplorer\formatHeader(), ilContainerSelectionExplorer\formatHeader(), ilSearchRootSelector\formatHeader(), ilECSNodeMappingLocalExplorer\formatHeader(), ilECSNodeMappingCmsExplorer\formatHeader(), ilPasteIntoMultipleItemsExplorer\formatHeader(), ilRepositoryExplorer\formatHeader(), ilECSNodeMappingLocalExplorer\formatObject(), ilPasteIntoMultipleItemsExplorer\formatObject(), ilExplorer\formatObject(), ilCalendarViewGUI\getContentByPlugins(), ilAbstractLearningHistoryProvider\getEmphasizedTitle(), ilRepositoryObjectSearchBlockGUI\getLegacyContent(), ilWikiFunctionsBlockGUI\getLegacyContent(), ilCalendarSelectionBlockGUI\getLegacyContent(), ilSCORMExplorer\getOutputIcons(), ilMailFormAttachmentPropertyGUI\insert(), ilAssLongmenuCorrectionsInputGUI\insert(), ilAssClozeTestCombinationVariantsInputGUI\insert(), ilAssSingleChoiceCorrectionsInputGUI\insert(), ilAssErrorTextCorrectionsInputGUI\insert(), ilOrgUnitAuthorityInputGUI\insert(), ilAssAnswerCorrectionsInputGUI\insert(), ilCustomInputGUI\insert(), ilHiddenInputGUI\insert(), ilAssMultipleChoiceCorrectionsInputGUI\insert(), ilAssMatchingPairCorrectionsInputGUI\insert(), ilImagemapCorrectionsInputGUI\insert(), ilEssayKeywordWizardInputGUI\insert(), ilDclGenericMultiInputGUI\insert(), ilFontSizeInputGUI\insert(), ilBackgroundImageInputGUI\insert(), ilNumericStyleValueInputGUI\insert(), ilManualPlaceholderInputGUI\insert(), ilRadioGroupInputGUI\insert(), ilFormSectionHeaderGUI\insert(), ilTextWizardInputGUI\insert(), ilWidthHeightInputGUI\insert(), ilSelectBuilderInputGUI\insert(), ilEMailInputGUI\insert(), ilUserLoginInputGUI\insert(), ilImageFileInputGUI\insert(), ilLocationInputGUI\insert(), ilNestedListInputGUI\insert(), ilCheckboxGroupInputGUI\insert(), ilAdvSelectInputGUI\insert(), ilBackgroundPositionInputGUI\insert(), ilTRBLBorderStyleInputGUI\insert(), ilKprimChoiceWizardInputGUI\insert(), ilNonEditableValueGUI\insert(), ilCheckboxInputGUI\insert(), ilMatrixRowWizardInputGUI\insert(), ilTRBLBorderWidthInputGUI\insert(), ilTRBLNumericStyleValueInputGUI\insert(), ilChatroomAuthInputGUI\insert(), ilDurationInputGUI\insert(), ilTagInputGUI\insert(), ilFileWizardInputGUI\insert(), ilTRBLColorPickerInputGUI\insert(), ilMatchingPairWizardInputGUI\insert(), ilColorPickerInputGUI\insert(), ilMultipleChoiceWizardInputGUI\insert(), ilCSSRectInputGUI\insert(), ilMultiSelectInputGUI\insert(), ilCombinationInputGUI\insert(), ilSelectInputGUI\insert(), ilScheduleInputGUI\insert(), ilRepositorySelectorInputGUI\insert(), ilExplorerSelectInputGUI\insert(), ilErrorTextWizardInputGUI\insert(), ilNumberInputGUI\insert(), ilDateTimeInputGUI\insert(), ilAnswerWizardInputGUI\insert(), ilRecurrenceInputGUI\insert(), ilMatchingWizardInputGUI\insert(), ilCategoryWizardInputGUI\insert(), ilImagemapFileInputGUI\insert(), ilPasswordInputGUI\insert(), ilTextAreaInputGUI\insert(), ilOrgUnitGenericMultiInputGUI\insert(), ilFileInputGUI\insert(), ilSingleChoiceWizardInputGUI\insert(), ilTextInputGUI\insert(), ilDateDurationInputGUI\insert(), ilLinkInputGUI\insert(), ilPCParagraphGUI\insertCharacteristicTable(), ilPCParagraphGUI\insertHelp(), ilSCORMExplorer\insertObject(), ilNestedList\listItemStart(), ilExplorerBaseGUI\listItemStart(), ilNestedList\listStart(), ilSurveyExecutionGUI\outNavigationButtons(), SurveyMetricQuestion\outPreconditionSelectValue(), ilFileInputGUI\outputSuffixes(), SurveyQuestionGUI\outQuestionText(), assMatchingQuestionGUI\populateAssignedTerms(), 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(), ilTestQuestionNavigationGUI\renderActionsIcon(), ilPollContentRenderer\renderAlertForAnonymousUser(), ilPollContentRenderer\renderAnchor(), ilPollContentRenderer\renderAnonimityInfo(), ilPollAnswersRenderer\renderAnswer(), ilPollContentRenderer\renderAvailability(), ilPollResultsRenderer\renderBarChart(), ilTestQuestionNavigationGUI\renderButtonInstance(), ilExplorerBaseGUI\renderChilds(), ilPollContentRenderer\renderComments(), ilAssLacLegendGUI\renderCommonLegendPart(), ilNewsTimelineGUI\renderDeleteModal(), ilPollContentRenderer\renderDescription(), ilContainerRenderer\renderDetails(), ilObjForumGUI\renderDraftContent(), ilNewsTimelineGUI\renderEditModal(), ilAssLacLegendGUI\renderExample(), ilContainerRenderer\renderHelperGeneric(), ilCalendarSelectionBlockGUI\renderItem(), ILIAS\Chatroom\BuildChat\renderLanguageVariables(), ILIAS\Exercise\Assignment\PanelBuilderUI\renderLinkList(), ilPollContentRenderer\renderMiscVoteInfo(), ilNestedList\renderNode(), ilExplorerBaseGUI\renderNode(), ilPollContentRenderer\renderNoQuestionMessage(), ilPollContentRenderer\renderNotAbleToVoteMessage(), ilPollContentRenderer\renderNotWithinVotingPeriodMessage(), ilObjForumGUI\renderPostContent(), ilObjForumGUI\renderPostingForm(), ilPollContentRenderer\renderQuestion(), ilAssLacLegendGUI\renderQuestSpecificExamples(), ilAssLacLegendGUI\renderQuestSpecificLegendPart(), ilChatroomViewGUI\renderRightUsersBlock(), ilContainerRenderer\renderSelectAllBlock(), ilChatroomViewGUI\renderSendMessageBox(), ilObjForumGUI\renderSplitButton(), ilPollResultsRenderer\renderStackedChart(), ilPollContentRenderer\renderTotalParticipantsInfo(), ilClozeGapInputBuilderGUI\setValueByArray(), ilPDNewsBlockGUI\showFeedUrl(), ilNewsForContextBlockGUI\showFeedUrl(), ilInfoScreenGUI\showLearningProgress(), and ilNewsForContextBlockGUI\showNews().

546  : void
547  {
548  if (is_array($variable)) {
549  $this->variableCache = array_merge(
550  $this->variableCache,
551  $variable
552  );
553  } else {
554  $this->variableCache[$variable] = $value;
555  }
556  }
+ 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 381 of file IT.php.

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

◆ touchBlock()

HTML_Template_IT::touchBlock ( string  $block)

Preserves an empty block even if removeEmptyBlocks is true.

Exceptions
ilTemplateException

Definition at line 578 of file IT.php.

References errorMessage().

578  : bool
579  {
580  if (!isset($this->blocklist[$block])) {
581  throw new ilTemplateException($this->errorMessage(self::IT_BLOCK_NOT_FOUND) . '"' . $block . "'");
582  }
583 
584  $this->touchedBlocks[$block] = true;
585 
586  return true;
587  }
errorMessage(int $value, string $blockname='')
Return a textual error message for a IT error code.
Definition: IT.php:835
+ Here is the call graph for this function:

Field Documentation

◆ $_options

array HTML_Template_IT::$_options
Initial value:
= [
'preserve_data' => false

$_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 305 of file IT.php.

◆ $block_cache

ILIAS Cache Container Container HTML_Template_IT::$block_cache
private

Definition at line 129 of file IT.php.

◆ $blockdata

array HTML_Template_IT::$blockdata = []

Array with the parsed content of a block.

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

Referenced by ilTemplate\init().

◆ $blocklist

array HTML_Template_IT::$blocklist = []

Array of all blocks and their content.

Definition at line 206 of file IT.php.

Referenced by findBlocks(), 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 158 of file IT.php.

◆ $blockparents

array HTML_Template_IT::$blockparents = []

Array of block parents.

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

◆ $blockvariables

array HTML_Template_IT::$blockvariables = []

Array of variables in a block.

Definition at line 216 of file IT.php.

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

◆ $clearCache

bool HTML_Template_IT::$clearCache = false

Clear cache on get()?

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

◆ $closingDelimiter

string HTML_Template_IT::$closingDelimiter = '}'

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

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

◆ $err

array HTML_Template_IT::$err = []

Contains the error objects.

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

◆ $flagBlocktrouble

bool HTML_Template_IT::$flagBlocktrouble = false

Internal flag indicating that a blockname was used multiple times.

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

◆ $flagGlobalParsed

bool HTML_Template_IT::$flagGlobalParsed = false

Flag indicating that the global block was parsed.

Definition at line 279 of file IT.php.

◆ $lastTemplatefile

string HTML_Template_IT::$lastTemplatefile = ''

EXPERIMENTAL! FIXME!

Definition at line 295 of file IT.php.

◆ $openingDelimiter

string HTML_Template_IT::$openingDelimiter = '{'

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

Definition at line 146 of file IT.php.

◆ $real_filename

array string HTML_Template_IT::$real_filename = ''
protected

Holds the real template file name.

Definition at line 313 of file IT.php.

◆ $removeEmptyBlocks

bool HTML_Template_IT::$removeEmptyBlocks = true

Controls the handling of empty blocks, default is remove.

Definition at line 186 of file IT.php.

Referenced by ilTemplate\loadTemplatefile(), and setTemplate().

◆ $removeUnknownVariables

bool HTML_Template_IT::$removeUnknownVariables = true

Controls the handling of unknown variables, default is remove.

Definition at line 181 of file IT.php.

Referenced by ilTemplate\loadTemplatefile(), and setTemplate().

◆ $removeVariablesRegExp

string HTML_Template_IT::$removeVariablesRegExp = ''

RegExp used to strip unused variable placeholder.

Definition at line 176 of file IT.php.

◆ $template

string HTML_Template_IT::$template = ''

Content of the template.

Definition at line 201 of file IT.php.

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

◆ $template_cache

ILIAS Cache Container Container HTML_Template_IT::$template_cache
private

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

◆ $variable_cache

ILIAS Cache Container Container HTML_Template_IT::$variable_cache
private

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

◆ IT_BLOCK_DUPLICATE

const HTML_Template_IT::IT_BLOCK_DUPLICATE = -4

Definition at line 126 of file IT.php.

◆ IT_BLOCK_NOT_FOUND

const HTML_Template_IT::IT_BLOCK_NOT_FOUND = -3

Definition at line 125 of file IT.php.

◆ IT_DEFAULT_BLOCK

const HTML_Template_IT::IT_DEFAULT_BLOCK = '__global__'

Definition at line 128 of file IT.php.

Referenced by ilTemplate\__construct().

◆ IT_ERROR

const HTML_Template_IT::IT_ERROR = -1

Definition at line 123 of file IT.php.

◆ IT_OK

const HTML_Template_IT::IT_OK = 1

Definition at line 122 of file IT.php.

◆ IT_TPL_NOT_FOUND

const HTML_Template_IT::IT_TPL_NOT_FOUND = -2

Definition at line 124 of file IT.php.

◆ IT_UNKNOWN_OPTION

const HTML_Template_IT::IT_UNKNOWN_OPTION = -6

Definition at line 127 of file IT.php.


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