Welcome to the Tutorial
Installation
Please read the Installation instructions in the README.md file on github
Concept of PbTpl
The concept of PbTpl as opposed to the most template engines with own languages is simple search replace without any logic in the templates. Even without using regular expressions.
This may sound a bit poor at first, but it depends on how you deal with it.
The concept of PbTpl is to fill small templates and fill it into templates of a higher level. The highest Template i.e. main.tpl or outer.tpl might have placeholder like {CONTENT}, {NAVIGATION} and {FOOTER}. The footer for expample could be filled with a string in $footerHtml. $footerHtml could be provided from the content of a text file or a constant or could be the result of an own Class that is using a seprarte footer.tpl file to provide the HTML string.
To make a long story short: The logic behind the concept of PbTpl lies in PHP and not in a template language. PHP works with template snippets that create independent HTML structures, which are then connected to form a larger whole.
PHP-Code
<?php
use PbClasses\PbTpl;
//put it in try-catch block!
$c = new PbTpl('content.tpl');
//$this is my template object with all
//This is may search-replace array.
//The keys are converted to upper case
//We recommend to set all keys to lower case where possible.
//If data comes from another source, e.g. from a database table,
//it can usually be taken over as it is.
$seRe = [
'first' => 'First replacement',
'second' => 'Second replacement'
];
//ATTENTION!
//Don't use different keys that are converted to the same search value!
//e.g. 'first' and 'First' and 'FIRST'. Only the first value would fill the placeholder!
$myRenderedHTML = $c->fillTpl('content', $seRe);
// If the template only contains a single placeholder,
// you can make it simpler
$html = $c->fillTpl('poor_template', 'single_placeholder', 'This is the text that will be insertetd.');
//If there is notheing to replace:
$nothingReplaced = $c->fillTpl('ready_element');
//======== TEMPLATES IN A LOOP=========//
//Key-value array:
$rowArr = [];
$rowArr[] = ['repl1' => 'a',
'repl2' => 'b',
'repl3' => 'c',
'repl4' => 'd'
];
$rowArr[] = ['repl1' => '1',
'repl2' => '2',
'repl3' => '3',
'repl4' => '4'
];
$rowArr[] = ['repl1' => '5',
'repl2' => '6',
'repl3' => '7',
'repl4' => '8'
];
$filledTpl = $c->fillRowTpl('more_replacements', $rowArr);
//Two arrays:
$search = array('repl1', 'repl2', 'repl3');
$rowReplArr = [];
$rowReplArr[] = ['b','c','d'];
$rowReplArr[] = ['1','2','2'];
$rowReplArr[] = ['4','5','6'];
$filledTpl2 = $c->fillRowTpl('more_replacements', $search, $rowReplArr);
//You can find more information in the examples and in
//vendor/pbieling/pbclasses-min/test/PbTplTest.php
Template-Code
[content]
# Command lines have a sharp as first char.
# Space before the sharp are allowed
####### Multiple sharps are allowed too. ############
# Empty lines are ignored.
!
Empty lines that should not be ignored are marked with an exclamation mark!
Placeholders are capitalized and enclosed in curly brackets:
This is a {PLACEHOLDER}.
First phone {PHONE_1}, second phone {PHONE_2}.
############################
[main]
<p>Template identifier stand alone in a row and are case sensitive.</p>
[td]
<td>{VALUE}</td>
[td_ID]
<td class="td-id"> data-id="{VALUE}">{VALUE}</td>
<strong>But be careful with terms that cause confusion.</strong>
#### Both [td_id] and [td_ID] are not a good idea.
\# This is no comment line, onley a sharp.
\!
The line above is no protected empty line but only a line, starting with a normal exclamation mark.