Download the PHP package affinity4/template without Composer
On this page you can find all versions of the php package affinity4/template. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download affinity4/template
More information about affinity4/template
Files in affinity4/template
Package template
Short Description Simple template engine with optional syntax which is easy to learn. Can use plain PHP also.
License MIT
Informations about the package template
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:
or
Syntax
Output a variables:
Set a variable:
To get an array item by key, such as $post['title']
:
If statement:
Foreach loop:
NOTE: Can be @foreach
also.
While loop:
For loop:
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
Then in you view:
File: views/home.php
Then simply render the view:
File: index.php
Usage
To render a template:
If you want to add new syntax you can use the addToken
method after initializing the template engine.
You can also pass a callable as the second argument to the addToken
method to use preg_replace_callback
instead for the replacement.
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
You then simply use dependency injection when calling the Template Engine class
The your layout template (views/layout/master.blade) can be:
In views/home.blade...
Tests
Run tests:
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.