https://github.com/teodoroleckie/template
⚡ Fast, powerful, scalable and customizable php template engine
https://github.com/teodoroleckie/template
easy-template fast-template php-8 php-template php-template-engine php8 template template-engine
Last synced: 1 day ago
JSON representation
⚡ Fast, powerful, scalable and customizable php template engine
- Host: GitHub
- URL: https://github.com/teodoroleckie/template
- Owner: teodoroleckie
- License: mit
- Created: 2021-04-21T15:36:11.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2021-05-13T09:22:23.000Z (almost 5 years ago)
- Last Synced: 2025-10-10T03:34:15.556Z (4 months ago)
- Topics: easy-template, fast-template, php-8, php-template, php-template-engine, php8, template, template-engine
- Language: PHP
- Homepage:
- Size: 49.8 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Fast and powerful php template engine
Syntax close to php for easy learning and management.
[](https://scrutinizer-ci.com/g/teodoroleckie/template/?branch=main)
[](https://scrutinizer-ci.com/code-intelligence)
[](https://scrutinizer-ci.com/g/teodoroleckie/template/build-status/main)
## Installation
You can install the package via composer:
```bash
composer require tleckie/template
```
### Printing variable:
```php
{{$name}}
```
Flexibility of spaces in variables:
```php
{{$name}}
{{ $user->getName() }}
{{
$user->getName()
}}
```
### Printing constant:
```php
{{CONSTANT}}
```
### Set variables:
```php
{set $variable = 'test'}
{set $variable = 355}
{set $userId = 25}
{set $userModel = new \MyNamespace\User($userId)}
{set $userModel = new \MyNamespace\User(25, 'John')}
```
### Dump:
```php
{dump $users}
```
### Comments:
```txt
{# {dump $users} #}
```
### Extends template:
```txt
{extends Common/Header.html}
List
{foreach $users as $user}
{{$user->getName()}}
{endforeach}
{extends Common/Footer.html}
```
If you add changes to the included templates as extensions, the compiler will not show these changes.
To remove the compiled files you have the "flushCompiled()" method
```php
$tpl = new Template(__DIR__.'/tpl/', '/var/www/cache/compiled/');
$tpl->flushCompiled();
```
You can enable development mode so that templates are always compiled.
```php
use Tleckie\Template\Template;
$tpl = new Template(
__DIR__.'/tpl/',
'/var/www/cache/compiled/',
null,
true // development mode
);
```
### Conditionals if, else & elseif:
```txt
{if $title === 'title'}
{{$title}}
{else}
It is not a headline!
{endif}
```
```txt
{if $type === 'Orange'}
{{$type}}
{elseif $title !== 'Apple'}
It is not an Apple!
{else}
Other fruit!
{endif}
```
```txt
{if $age >= 18}
Yes!
{else}
Ups :)
{endif}
```
```txt
{if $age !== 18}
Yes!
{else}
Ups :)
{endif}
```
### Nested loops:
Foreach and for supported.
```txt
{foreach $data as $key => $persons}
{foreach $persons as $id => $name}
{{$name}}:{{ $key }}
{endforeach}
{endforeach}
```
### Helpers:
Create your own helpers to invoke on the template.
```php
{{$this->arrayHelper::last($persons)}}
```
### Create the template instance:
```php
['Marcos', 'John', 'Pedro', 'Marta'],
'title' => 'User list',
'users' => [
new User('Marcos'), new User('John'), new User('Pedro')
]
];
$tpl->render('List/Users.html', $data);
```
Template List/Users.html
```txt
{foreach $users as $user}
{{ strtoupper($user->getName()) }}
{endforeach}
```
### Rules:
The Tleckie\Template\Compiler\Parser\Rules class establishes the rules for handling templates.
You can create new rules to enrich your templating engine through the RuleInterface interface.
```php
use Tleckie\Template\Template;
use Tleckie\Template\Compiler\Compiler;
use Tleckie\Template\Compiler\Parser\Rules;
$rules = [
new Rules(),
new MyOwnRules()
];
$compiler = new Compiler($rules);
$tpl = new Template(
__DIR__.'/tpl/',
'/var/www/cache/compiled/',
$compiler
);
```
### Helpers:
You can create your own helpers to be invoked from templates to do complex tasks, manipulate objects or strings.
The Template class has a method for adding helpers. Note that by this mechanism you can also add your dependency injector.
```php
registerHelper(
'arrayHelper',
new \MyNamespace\Infrastructure\Helpers\ArrayHelper()
);
```
Each helper added to the template object must have an alias to be invoked through it.
```php
{{$this->arrayHelper::last($persons)}}
```
Helper example:
```php
*/
class ArrayHelper
{
public static function last(array $array)
{
return end($array);
}
}
```
That's all! I hope this helps you ;)