PHP-Template-Engine
![Software License][ico-license]
![Coverage Status][ico-scrutinizer]
![Total Downloads][ico-downloads], (*1)
Table of Contents
Prerequisites
Installation
``` bash
$ composer require EdsonOnildoJR/PHP-Template-Engine, (*2)
Basic usage
-----------
Create an **index.php** file and require the autoload.php of composer
``` php
<?php
include 'vendor/autoload.php';
After that, let's to do all necessary configuration, (*3)
``` php
use Sketch\Tpl;, (*4)
Tpl::config([
//'environment' => 'production',
'environment' => 'development',
'template_dir' => 'path/to/templates',
'cache_dir' => 'path/to/caches'
]);, (*5)
Assign and render template
``` php
Tpl::assing('title', 'Hello!');
Tpl::render('test');
Variables
Variables are the dynamic content of the template, valorized on the execution of the script with Tpl::assing() static method. Variables names are case sensitive., (*6)
Template:
``` html
Welcome to {title}, (*7)
**Data:**
``` php
<?php
Tpl::assign('title', 'Sketch');
Output:
``` html
Welcome to Sketch, (*8)
### Modifiers on variables
You can add modifiers that are executed on the variables.
**Template:**
``` html
Hello {name|capitalize}!
Data:
``` php
<?php, (*9)
Tpl::assign('name', 'edson onildo');, (*10)
**Output:**
``` html
Hello Edson Onildo!
Conditional Expression
Checks an expression and print the code between {if}{else} if the conditions is true or {else}{/if} if the condition is false. Try to use nested blocks :), (*11)
Template:
``` html
{if age >= 18}
Adult
{else}
Minor
{/if}, (*12)
**Data:**
``` php
<?php
Tpl::assign('age', 19);
Output:
``` html
Adult, (*13)
you can also use the {if condition}content{elseif condition}content{else}content{/if} or any combination of if and else.
### Loop
Allow to loop through the value of arrays or objects.
**Template:**
``` html
<ul>
{loop authors as author}
<li>
{author.name}: {author.page}
</li>
{/loop}
</ul>
Data:
``` php
<?php, (*14)
$authors = [
[
'name' => 'Edson Onildo',
'page' => 'https://github.com/EdsonOnildoJR'
],
[
'name' => 'Contributors',
'page' => 'https://github.com/EdsonOnildoJR/Sketch/contributors'
]
];, (*15)
Tpl::assign('authors', $authors);, (*16)
**Output:**
``` html
Edson Onildo: https://github.com/EdsonOnildoJR
Contributors: https://github.com/EdsonOnildoJR/Sketch/contributors
Function
Use {func funcname()} tag to execute a PHP function and print the result. You can pass strings, numbers and variables as parameters., (*17)
Template:
``` html
{func date('Y')}, (*18)
**Output:**
``` html
2018
Include
With {include 'template'} tag you can include external template as blocks., (*19)
Template:
``` html
, (*20)
New user:
{template 'userForm'}
**Output:**
``` html
<h1>New user:</h1>
<form class="user" action="" method="post">
...
</form>
Change log
Please see CHANGELOG for more information on what has changed recently., (*21)
Testing
bash
$ composer test
, (*22)
Contributing
Please see CONTRIBUTING and CODE_OF_CONDUCT for details., (*23)
Security
If you discover any security related issues, please email inbox.edsononildo@gmail.com instead of using the issue tracker., (*24)
Credits
License
The MIT License (MIT). Please see License File for more information., (*25)