https://github.com/affinity4/template
Simple extendable template engine with optional syntax which is easy to learn. Allows you to use plain PHP also.
https://github.com/affinity4/template
affinity4 php template-engine template-language
Last synced: 5 months ago
JSON representation
Simple extendable template engine with optional syntax which is easy to learn. Allows you to use plain PHP also.
- Host: GitHub
- URL: https://github.com/affinity4/template
- Owner: affinity4
- License: mit
- Created: 2017-05-14T23:29:24.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2024-02-18T14:26:43.000Z (over 2 years ago)
- Last Synced: 2025-05-22T11:39:35.007Z (about 1 year ago)
- Topics: affinity4, php, template-engine, template-language
- Language: PHP
- Homepage:
- Size: 39.1 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Template
Full-featured template engine with optional syntax which is easy to learn. Can use plain PHP also.
## Features
- HTML Comment syntax
- Can use plain PHP in templates if needed
- Add new syntax if needed.
## Installation
Affinity4/Template is available via composer:
```bash
composer require affinity4/template
```
or
```json
{
"require": {
"affinity4/template": "^1.1"
}
}
```
## Syntax
Output a variables:
```html
```
Set a variable:
```html
```
To get an array item by key, such as `$post['title']`:
```html
```
If statement:
```html
Something
Default Title
```
Foreach loop:
```html
```
__NOTE:__ Can be `@foreach` also.
While loop:
```html
Number:
```
For loop:
```html
Number:
```
## Layouts and Blocks
You can extend master layouts the same way as you would in any other template engine such as Twig or Blade.
Create a master layout with sections to be overridden in each view file:
File: `views/layout/master.php`
```html
This can be overridden: Site description
Default Page
Page content goes here...
```
Then in you view:
File: `views/home.php`
```html
This is the homepage
```
Then simply render the view:
File: `index.php`
```php
use Affinity4\Template;
$template = new Template\Engine(new Template\Syntax);
$tempalte->render('views/home.php', ['page_title' => 'Home']);
```
## Usage
To render a template:
```php
use Affinity4\Template;
$template = new Template\Engine(new Template\Syntax);
$template->render('views/home.php', ['title' => 'Home Page']);
```
If you want to add new syntax you can use the `addToken` method after initializing the template engine.
```php
use Affinity4\Template;
$template = new Template\Engine(new Template\Syntax);
$template->addRule('/\{\{ ([\w\d+]) \}\}/', '= $$1 ?>');
$template->render('views/home.php', ['title' => 'Home Page']);
```
You can also pass a callable as the second argument to the `addToken` method to use `preg_replace_callback` instead for the replacement.
```php
use Affinity4\Template;
$template = new Template\Engine(new Template\Syntax);
$template->addRule('/\{\{ ([\w\d]+) \}\}/', function ($statement) {
return '';
});
$template->render('views/home.php', ['title' => 'Home Page']);
```
## Overriding Syntax
One thing which is quite original about Affinity4 Template is that it allows you to replace the Syntax class with your won simple class to create a template language of your own.
It easy to add all the features currently in Affinity4 Template by simply extending the Affinity4\Template\Tokenizer class and implementing the Affinity4\Template\SyntaxInterface. From there you need only add rules for variables, loops etc. and extend and block syntax. If you do not add extend or block rules that functionality will simply not be available.
Here is an example of creating a Blade style syntax of your own
```php
addRule('/\{\{ ?(\$.*) ?\}\}/', '');
$this->addRule('/@if ?\(\(.*))/', '');
$this->addRule('/@elseif ?\(\(.*))/', '');
$this->addRule('/@else/', '');
$this->addRule('/@endif/', '');
$this->addRule('/@foreach ?\(\(.*))/', '');
$this->addRule('/@endforeach/', '');
// For, while etc...
$this->addExtendsRule('/@extends\((.*)\)/');
$this->addSectionRule('/@section\('(.*)'\)/', '/@endsection/');
}
}
```
You then simply use dependency injection when calling the Template Engine class
```php
require_once __DIR__ . '/vendor/autoload.php';
use Affinity4\Template\Engine;
use Your\Template\Syntax\Blade2;
$blade2 = new Engine(new Blade2);
$blade2->render('views/home.blade', ['title' => 'Blade 2']);
```
The your layout template (views/layout/master.blade) can be:
```html
@section('title') Default to be overriden @endsection
@section('title')
Title here...
@endsection
```
In views/home.blade...
```html
@extends('layouts/master.blade')
@section('title')
{{ $title }}
@endsection
```
## Tests
Run tests:
```bash
vendor/bin/phpunit
```
## Licence
(c) 2017 Luke Watts (Affinity4.ie)
This software is licensed under the MIT license. For the
full copyright and license information, please view the
LICENSE file that was distributed with this source code.