bonfim/tpl

Gerenciador de Templates PHP

v2.2.2 2018-09-09 20:00 UTC

This package is auto-updated.

Last update: 2024-04-29 04:07:18 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

Table of Contents

Prerequisites

  • PHP 7.1+

Installation

$ composer require EdsonOnildoJR/PHP-Template-Engine

Basic usage

Create an index.php file and require the autoload.php of composer

<?php

include 'vendor/autoload.php';

After that, let's to do all necessary configuration

use Sketch\Tpl;

Tpl::config([
    //'environment' => 'production',
    'environment'   => 'development',
    'template_dir'  => 'path/to/templates',
    'cache_dir'     => 'path/to/caches'
]);

Assign and render template

Tpl::assing('title', 'Hello!');
Tpl::render('test');

Sketch Tags

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.

Template:

Welcome to {title}

Data:

<?php

Tpl::assign('title', 'Sketch');

Output:

Welcome to Sketch

Modifiers on variables

You can add modifiers that are executed on the variables.

Template:

Hello {name|capitalize}!

Data:

<?php

Tpl::assign('name', 'edson onildo');

Output:

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 :)

Template:

{if age >= 18}
    Adult
{else}
    Minor
{/if}

Data:

<?php

Tpl::assign('age', 19);

Output:

Adult

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:

<ul>
    {loop authors as author}
    <li>
        {author.name}: {author.page}
    </li>
    {/loop}
</ul>

Data:

<?php

$authors = [
    [
        'name' => 'Edson Onildo',
        'page' => 'https://github.com/EdsonOnildoJR'
    ],
    [
        'name' => 'Contributors',
        'page' => 'https://github.com/EdsonOnildoJR/Sketch/contributors'
    ]
];

Tpl::assign('authors', $authors);

Output:

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.

Template:

{func date('Y')}

Output:

2018

Include

With {include 'template'} tag you can include external template as blocks.

Template:

<h1>New user:</h1>
{template 'userForm'}

Output:

<h1>New user:</h1>
<form class="user" action="" method="post">
    ...
</form>

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email inbox.edsononildo@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.