PHPackages                             affinity4/template - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. affinity4/template

ActiveLibrary[Templating &amp; Views](/categories/templating)

affinity4/template
==================

Simple template engine with optional syntax which is easy to learn. Can use plain PHP also.

1.3.1(3y ago)328MITPHPPHP &gt;=7.4

Since May 15Pushed 2y ago1 watchersCompare

[ Source](https://github.com/affinity4/template)[ Packagist](https://packagist.org/packages/affinity4/template)[ RSS](/packages/affinity4-template/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (1)Dependencies (2)Versions (9)Used By (0)

Template
========

[](#template)

Full-featured template engine with optional syntax which is easy to learn. Can use plain PHP also.

Features
--------

[](#features)

- HTML Comment syntax
- Can use plain PHP in templates if needed
- Add new syntax if needed.

Installation
------------

[](#installation)

Affinity4/Template is available via composer:

```
composer require affinity4/template
```

or

```
{
    "require": {
        "affinity4/template": "^1.1"
    }
}
```

Syntax
------

[](#syntax)

Output a variables:

```

```

Set a variable:

```

```

To get an array item by key, such as `$post['title']`:

```

```

If statement:

```

    Something

    Default Title

```

Foreach loop:

```

```

**NOTE:** Can be `@foreach` also.

While loop:

```

    Number:

```

For loop:

```

    Number:

```

Layouts and Blocks
------------------

[](#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`

```
>

    This can be overridden: Site description

        Default Page

        Page content goes here...

```

Then in you view:

File: `views/home.php`

```

This is the homepage

```

Then simply render the view:

File: `index.php`

```
use Affinity4\Template;

$template = new Template\Engine(new Template\Syntax);

$tempalte->render('views/home.php', ['page_title' => 'Home']);
```

Usage
-----

[](#usage)

To render a template:

```
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.

```
use Affinity4\Template;

$template = new Template\Engine(new Template\Syntax);

$template->addRule('/\{\{ ([\w\d+]) \}\}/', '');

$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.

```
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
-----------------

[](#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

```
