ILIAS  release_8 Revision v8.24
class.ilTemplate.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
5/* Copyright (c) 1998-2022 ILIAS open source, Extended GPL, see docs/LICENSE */
6
7require_once __DIR__ . '/../lib/html-it/IT.php';
8require_once __DIR__ . '/../lib/html-it/ITX.php';
9
17{
21 public array $vars = [];
22
28 public string $activeBlock = '';
29
30 protected static array $il_cache = [];
31
32 protected bool $il_use_cache;
33
34 protected string $il_cur_key;
35
36 protected string $tplName;
37
38 protected string $tplPath;
39
40 protected string $tplIdentifier;
41
54 public function __construct(
55 string $file,
56 bool $flag1,
57 bool $flag2,
58 string $in_module = "",
60 bool $plugin = false,
61 bool $a_use_cache = true
62 ) {
63 $this->activeBlock = HTML_Template_IT::IT_DEFAULT_BLOCK;
64 $this->il_use_cache = $a_use_cache;
65 $this->il_cur_key = $file . "/" . $in_module;
66
67 $fname = $this->getTemplatePath($file, $in_module);
68 if (!file_exists($fname)) {
69 throw new ilTemplateException("Template '$fname' was not found.");
70 }
71
72 $this->tplName = basename($fname);
73 $this->tplPath = dirname($fname);
74 $this->vars["TPLPATH"] = $this->tplPath;
75 $this->tplIdentifier = $this->getTemplateIdentifier($file, $in_module);
76
78
79 $this->loadTemplatefile($fname, $flag1, $flag2);
80 $this->setOption('use_preg', false);
81 }
82
83 protected function init(): void
84 {
85 $this->free();
86 $this->buildFunctionlist();
87
88 $cache_hit = false;
89 if ($this->il_use_cache &&
90 isset(self::$il_cache[$this->il_cur_key]) &&
91 is_array(self::$il_cache[$this->il_cur_key])
92 ) {
93 $cache_hit = true;
94 $this->err = self::$il_cache[$this->il_cur_key]["err"];
95 $this->flagBlocktrouble = self::$il_cache[$this->il_cur_key]["flagBlocktrouble"];
96 $this->blocklist = self::$il_cache[$this->il_cur_key]["blocklist"];
97 $this->blockdata = self::$il_cache[$this->il_cur_key]["blockdata"];
98 $this->blockinner = self::$il_cache[$this->il_cur_key]["blockinner"];
99 $this->blockparents = self::$il_cache[$this->il_cur_key]["blockparents"];
100 $this->blockvariables = self::$il_cache[$this->il_cur_key]["blockvariables"];
101 }
102
103 if (!$cache_hit) {
104 $this->findBlocks($this->template);
105 $this->buildBlockvariablelist();
106 if ($this->il_use_cache) {
107 self::$il_cache[$this->il_cur_key]["err"] = $this->err;
108 self::$il_cache[$this->il_cur_key]["flagBlocktrouble"] = $this->flagBlocktrouble;
109 self::$il_cache[$this->il_cur_key]["blocklist"] = $this->blocklist;
110 self::$il_cache[$this->il_cur_key]["blockdata"] = $this->blockdata;
111 self::$il_cache[$this->il_cur_key]["blockinner"] = $this->blockinner;
112 self::$il_cache[$this->il_cur_key]["blockparents"] = $this->blockparents;
113 self::$il_cache[$this->il_cur_key]["blockvariables"] = $this->blockvariables;
114 }
115 }
116
117 // we don't need it anymore
118 $this->template = '';
119 }
120
121 public function blockExists(string $a_blockname): bool
122 {
123 // added second evaluation to the return statement because the first one
124 // only works for the content block (Helmut Schottmüller, 2007-09-14).
125 return
126 isset($this->blockvariables["content"][$a_blockname]) ||
127 isset($this->blockvariables[$a_blockname]);
128 }
129
130 public function get(string $part = ilGlobalTemplateInterface::DEFAULT_BLOCK): string
131 {
132 global $DIC;
133
134 $html = $this->getUnmodified($part);
135 $component_factory = $DIC["component.factory"];
136 foreach ($component_factory->getActivePluginsInSlot("uihk") as $ui_plugin) {
137 $gui_class = $ui_plugin->getUIClassInstance();
138 $resp = $gui_class->getHTML(
139 "",
140 "template_get",
141 [
142 "tpl_id" => $this->tplIdentifier,
143 "tpl_obj" => $this,
144 "html" => $html
145 ]
146 );
147
148 if (ilUIHookPluginGUI::KEEP !== $resp["mode"]) {
149 $html = $gui_class->modifyHTML($html, $resp);
150 }
151 }
152
153 return $html;
154 }
155
159 public function getUnmodified(string $part = ilGlobalTemplateInterface::DEFAULT_BLOCK): string
160 {
161 // I can't believe how garbage this is.
164 }
165
166 return parent::get($part);
167 }
168
172 public function setCurrentBlock(string $part = ilGlobalTemplateInterface::DEFAULT_BLOCK): bool
173 {
174 // I can't believe how garbage this is.
177 }
178
179 $this->activeBlock = $part;
180 return parent::setCurrentBlock($part);
181 }
182
186 public function touchBlock(string $block): bool
187 {
188 $this->setCurrentBlock($block);
189 $count = $this->fillVars();
190 $this->parseCurrentBlock();
191
192 if (0 === $count) {
193 return parent::touchBlock($block);
194 }
195
196 return false;
197 }
198
203 {
204 $this->fillVars();
205 $this->activeBlock = self::IT_DEFAULT_BLOCK;
206
207 return parent::parseCurrentBlock();
208 }
209
210 public function addBlockFile(string $var, string $block, string $tplname, string $in_module = null): bool
211 {
212 global $DIC;
213
214 if (DEBUG) {
215 echo "<br/>Template '" . $this->tplPath . "/" . $tplname . "'";
216 }
217
218 $tplfile = $this->getTemplatePath($tplname, $in_module);
219 if (file_exists($tplfile) === false) {
220 echo "<br/>Template '" . $tplfile . "' doesn't exist! aborting...";
221 return false;
222 }
223
224 $id = $this->getTemplateIdentifier($tplname, $in_module);
225 $template = $this->getFile($tplfile);
226 $component_factory = $DIC["component.factory"];
227 foreach ($component_factory->getActivePluginsInSlot("uihk") as $ui_plugin) {
228 $gui_class = $ui_plugin->getUIClassInstance();
229 $resp = $gui_class->getHTML(
230 "",
231 "template_add",
232 [
233 "tpl_id" => $id,
234 "tpl_obj" => $this,
235 "html" => $template,
236 ]
237 );
238
239 if ($resp["mode"] !== ilUIHookPluginGUI::KEEP) {
240 $template = $gui_class->modifyHTML($template, $resp);
241 }
242 }
243
244 return $this->addBlock($var, $block, $template);
245 }
246
251 private function fillVars(): int
252 {
253 $count = 0;
254 foreach ($this->vars as $key => $val) {
255 if (is_array($this->blockvariables[$this->activeBlock]) &&
256 array_key_exists($key, $this->blockvariables[$this->activeBlock])
257 ) {
258 $this->setVariable($key, $val);
259 $count++;
260 }
261 }
262
263 return $count;
264 }
265
266 public function loadTemplatefile(
267 string $filename,
268 bool $removeUnknownVariables = true,
269 bool $removeEmptyBlocks = true
270 ): bool {
271 global $DIC;
272
273 $template = '';
274 if (!$this->flagCacheTemplatefile ||
275 $this->lastTemplatefile !== $filename
276 ) {
277 $template = $this->getFile($filename);
278 }
279 $this->lastTemplatefile = $filename;
280
281 $component_factory = $DIC["component.factory"];
282 foreach ($component_factory->getActivePluginsInSlot("uihk") as $ui_plugin) {
283 $gui_class = $ui_plugin->getUIClassInstance();
284 $resp = $gui_class->getHTML(
285 "",
286 "template_load",
287 [
288 "tpl_id" => $this->tplIdentifier,
289 "tpl_obj" => $this,
290 "html" => $template,
291 ]
292 );
293
294 if ($resp["mode"] !== ilUIHookPluginGUI::KEEP) {
295 $template = $gui_class->modifyHTML($template, $resp);
296 }
297 }
298
299 return
300 $template !== '' &&
301 $this->setTemplate(
302 $template,
305 );
306 }
307
311 protected function getTemplatePath(string $a_tplname, string $a_in_module = null): string
312 {
313 $fname = "";
314 if (strpos($a_tplname, "/") === false) {
315 $module_path = "";
316
317 if ($a_in_module !== "") {
318 $module_path = $a_in_module . "/";
319 }
320
321 // use ilStyleDefinition instead of account to get the current skin
322 if (ilStyleDefinition::getCurrentSkin() !== "default") {
324
325 $fname = "./Customizing/global/skin/" .
326 ilStyleDefinition::getCurrentSkin() . "/" . $style . "/" . $module_path
327 . basename($a_tplname);
328
329 if ($fname === "" || !file_exists($fname)) {
330 $fname = "./Customizing/global/skin/" .
331 ilStyleDefinition::getCurrentSkin() . "/" . $module_path . basename($a_tplname);
332 }
333 }
334
335 if ($fname === "" || !file_exists($fname)) {
336 $fname = "./" . $module_path . "templates/default/" . basename($a_tplname);
337 }
338 } elseif (strpos($a_tplname, "src/UI") === 0) {
339 if (class_exists("ilStyleDefinition") // for testing
340 && ilStyleDefinition::getCurrentSkin() != "default") {
343 $base_path = "./Customizing/global/skin/";
344 $ui_path = "/" . str_replace("src/UI/templates/default", "UI", $a_tplname);
345 $fname = $base_path . ilStyleDefinition::getCurrentSkin() . "/" . $style . "/" . $ui_path;
346
347 if (!file_exists($fname)) {
348 $fname = $base_path . $skin . "/" . $ui_path;
349 }
350 }
351
352 if ($fname == "" || !file_exists($fname)) {
353 $fname = $a_tplname;
354 }
355 } else {
356 $fname = $a_tplname;
357 }
358 return $fname;
359 }
360
367 public function getTemplateIdentifier(string $a_tplname, string $a_in_module = null): string
368 {
369 if (strpos($a_tplname, "/") === false) {
370 if (null !== $a_in_module) {
371 $module_path = $a_in_module . "/";
372 } else {
373 $module_path = "";
374 }
375
376 return $module_path . basename($a_tplname);
377 }
378
379 return $a_tplname;
380 }
381
382 public function variableExists(string $a_variablename): bool
383 {
384 return isset($this->blockvariables["content"][$a_variablename]);
385 }
386}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
$filename
Definition: buildRTE.php:78
Integrated Template Extension - ITX With this class you get the full power of the phplib template cla...
Definition: ITX.php:32
addBlock(string $placeholder, string $blockname, string $template)
Adds a block to the template changing a variable placeholder to a block placeholder.
Definition: ITX.php:173
buildFunctionlist()
Builds a functionlist from the template.
Definition: ITX.php:272
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 $blockparents
Array of block parents.
Definition: IT.php:195
bool $removeEmptyBlocks
Controls the handling of empty blocks, default is remove.
Definition: IT.php:160
const IT_DEFAULT_BLOCK
Definition: IT.php:105
setOption(string $option, $value)
Sets the option for the template class.
Definition: IT.php:321
array $blockinner
Array of inner blocks of a block.
Definition: IT.php:200
string $template
Content of the template.
Definition: IT.php:175
findBlocks(string $string)
Recusively builds a list of all blocks within the template.
Definition: IT.php:705
array $blockdata
Array with the parsed content of a block.
Definition: IT.php:185
getFile(string $filename)
Reads a file from disk and returns its content.
Definition: IT.php:750
parseCurrentBlock()
Parses the current block.
Definition: IT.php:499
setTemplate(string $template, bool $removeUnknownVariables=true, bool $removeEmptyBlocks=true)
Sets the template.
Definition: IT.php:616
array $blocklist
Array of all blocks and their content.
Definition: IT.php:180
array $err
Contains the error objects.
Definition: IT.php:110
array $blockvariables
Array of variables in a block.
Definition: IT.php:190
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:514
bool $flagBlocktrouble
Internal flag indicating that a blockname was used multiple times.
Definition: IT.php:248
bool $removeUnknownVariables
Controls the handling of unknown variables, default is remove.
Definition: IT.php:155
static getCurrentSkin()
get the current skin use always this function instead of getting the account's skin the current skin ...
static getCurrentStyle()
get the current style or sub style use always this function instead of getting the account's style th...
special template class to simplify handling of ITX/PEAR
variableExists(string $a_variablename)
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
init()
Clears all datafields of the object and rebuild the internal blocklist LoadTemplatefile() and setTemp...
static array $il_cache
addBlockFile(string $var, string $block, string $tplname, string $in_module=null)
__construct(string $file, bool $flag1, bool $flag2, string $in_module="", string $vars=ilGlobalTemplateInterface::DEFAULT_BLOCK, bool $plugin=false, bool $a_use_cache=true)
constructor ilTemplate constructor.
string $il_cur_key
string $activeBlock
Aktueller Block Der wird gemerkt bei der berladenen Funktion setCurrentBlock, damit beim ParseBlock v...
blockExists(string $a_blockname)
Checks wheter a block exists.
fillVars()
all template vars defined in $vars will be replaced automatically without setting and parsing them wi...
getTemplatePath(string $a_tplname, string $a_in_module=null)
string $tplIdentifier
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
loadTemplatefile(string $filename, bool $removeUnknownVariables=true, bool $removeEmptyBlocks=true)
Reads a template file from the disk.
array $vars
variablen die immer in jedem block ersetzt werden sollen
getUnmodified(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
getTemplateIdentifier(string $a_tplname, string $a_in_module=null)
get a unique template identifier The identifier is common for default or customized skins but distinc...
touchBlock(string $block)
global $DIC
Definition: feed.php:28
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
string $key
Consumer key/client ID value.
Definition: System.php:193
get(string $key, Refinery\Transformation $t)
Get passed parameter, if not data passed, get key from http request.