PHPlater
A simple PHP template engine that lets PHP do all the logic and then append it to the HTML in the template file. It is set to solve the problem of mixing too much logic into the template file itself and by that loosing some control over where the logic happens. You also get a bit more control over the data that is passed down to the template, and the code is easier to manage and to track.
Installation
PHP 8.0 or higher is needed for this class to work.
Use the package manager composer to install PHPlater.
composer install phplater/phplater
Simple Usage
Given this PHP code
$phplater = new PHPlater();
$phplater->plate('hello', 'world!');
echo $phplater->render('Hello {{hello}}');
This will be the output:
Hello world!
Advanced Usage
Some more examples are available under /examples and /tests
Given this template.tpl file
<div>
One: {{ one }}<br />
Two: {{ two.getTwo }}<br />
Three: {{ assoc.three }}<br />
Four: {{ assoc.4 }}<br />
Five: {{ assoc.5.0 }}<br />
Six: {{ assoc.six.0 }}<br />
Seven: {{ seven_obj.returnArray.seven }}
</div>
And this PHP code
class Test {
function getTwo() {
return 'Kaksi';
}
public function returnArray() {
return ['seven' => 'Seitsemän'];
}
}
$phplater = new PHPlater();
$phplater->plates([
'one' => 'Yksi',
'two' => new Test(),
'assoc' => [
'three' => 'Kolme',
4 => 'Neljä',
5 => ['Viisi'],
'six' => ['Kuusi']
],
'seven_obj' => new Test()
]);
echo $phplater->render('template.tpl');
This will be the output:
<div>
One: Yksi<br />
Two: Kaksi<br />
Three: Kolme<br />
Four: Neljä<br />
Five: Viisi<br />
Six: Kuusi<br />
Seven: Seitsemän
</div>
Test
Run the following
vendor\bin\phpunit tests/PHPlaterTest.php
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
Documentation
PHPDocumentator v.3.0 is used for generating documentation from PHPDoc in code. Download phpDocumentor.phar and run the following in the project folder to update it.
php c:/path/to/phpDocumentor.phar -d src -t docs