irfantoor/template-engine

A simple, small and smart template engine

0.3.3 2023-02-06 18:12 UTC

This package is auto-updated.

Last update: 2024-10-06 22:09:04 UTC


README

A simple and small template engine.

Quick start

Installation

Installation or inclusion in your project:

$ composer require irfantoor/template-engine

To test the template engine:

$ vendor/bin/test

Creating the Template Engine:

$te = new IrfanTOOR\TemplateEngine([
    'max_depth' => 3, # defaults to 3
    'base_path'  => 'your/path/to/template/files/',
]);

Processing Text

$text = "{$greeting} {$user}!";
$data = [
    'greeting' => 'Hello',
    'user' => 'World',
];

echo $te->processText($text, $data);

Processing File

# home.php
<h1>{$title}</h1>

<ul>
{@foreach ($list as $item):}
<li>{$item}</li>
{@endforeach}
</ul>
$data = [
    'title' => 'Fruits',
    'list' => [
        'Apple',
        'Orange',
        'Blackberry',
        'Raspberry',
    ],
];

echo $te->processFile("home.php", $data);

Template

Comments

format: {#...}

{# its a comment!}
{#anything here including the external brackets are removed from the output}

Tokens

format: {$...}

The tokens are replaced with the values provided by the passed data array.

{$name['first']} {$name['last']}
{$address[0]}
{$address[1]}
tel: {$tel}
email: {$email}

format: {!$...} The tokens are replaced with tags are replaced with value, without doing any html special character conversion. It helps in including the html tags etc. which are displayed as html and not as content.

Commands

format: {@...}

{@include 'header.php'}

{@echo date('d-m-Y')}

{@if ($list):}
    # Note: you can use the curley brackets, so use the form foreach (...): endforeach instead
    {@foreach ($list as $k => $v):}
    data provided is : {$k} | {$v}
    {@endforeach}
{@endif}



# you can define the data in the template
{@ $d = date('d-m-Y')}

# and prints ...
date : {$d}

# Note: The statement in {@ ...} tags need not to be terminated with a semicolon ';'
{@ $list = [
    'black',
    'white'
]}

dump list:
# Note: The variable to dump, might as well be an object, array, bool int or a string
{$list}

Note: after {@ use the commands as if you were using the php code, though only constraint is that you can not use the loops or commands using the curly brackets.