Example 2 - Template composition
A template file contains one or more templates that should - but do not have to - be related in terms of content. The outermost template should be at the top or alone in a separate file if used in different contexts.
A template object contains all templates of the template file (or a corresponding string) passed to the constructor.
The use of the individual templates, e.g. for navigation or a content area or for rows or cells in a table, as well as the assembly of the page does not result from the template itself but must be controlled by the program logic.
The purpose of use can be documented through comments in the templates and naming of the template identifiers. But that is not binding.
Creative uses are also possible, as the example shows.
Juggling with templates
The time is
20:39:22Variation of the time block
This is a text that is displayed if seconds divided by 3 has a remainder of 1.
Control value = 1
PHP-Code
<?php
use PbClasses\PbTpl;
try {
$c = new PbTpl('./templates/content_composition.tpl');
$time = date('H:i:s');
$seconds = (int) substr($time, -2);
$remainder = $seconds % 3;
$seRe = [
'example_headline' => 'Juggling with templates',
'time_output' => $c->fillTpl('time_output', 'time', $time),
'time_dependent_text_block' => $c->fillTpl('text_block_' . $remainder , 'control_value', $remainder)
];
return $c->fillTpl('content', $seRe);
} catch (\Exception $exc) {
echo $exc->getMessage();
exit;
}
Template-Code
[content]
<h4>{EXAMPLE_HEADLINE}</h4>
#### This will be filled withe the template time_output}
{TIME_OUTPUT}
<div class="timd-dempendent-text-block">
{TIME_DEPENDENT_TEXT_BLOCK}
</div>
<a href="example_02.php">Refresh</a>
###########
[time_output]
<div class="time-output">
<h5>The time is</h5>
<strong>{TIME}</strong>
</div>
#### Alternative text blocks
[text_block_0]
<p>This is a text that is displayed if seconds divided by 3 has a remainder of 0.<br>
Control value = {CONTROL_VALUE}</p>
[text_block_1]
<h5>Variation of the time block</h5>
<p>This is a text that is displayed if seconds divided by 3 has a remainder of 1.<br>
Control value = {CONTROL_VALUE}</p>
[text_block_2]
<div style="border:2px dotted red">
<p>This is a text that is displayed if seconds divided by 3 has a remainder of 1.<br>
Control value = {CONTROL_VALUE}</p></div>