alexkuusk/malli

There is no license information available for the latest version (v0.1) of this package.

Slim PHP template parser

v0.1 2024-02-10 18:37 UTC

This package is auto-updated.

Last update: 2024-09-19 21:34:22 UTC


README

A simple and lightweight PHP templating engine using few custom tags that all are translated into pure PHP. PHP is also allowed inside templates, just be aware of the scopes. Template blocks are compiled into PHP functions and functions are called and their output captured.

Possibility to add custom tags, for example translation code. That code's output can be turned into static and saved along with PHP code, so that heavy translating functions are not called every page load. Supports multilanguage and multisite, just pass language parameter and use dynamic path with another set of templates ('path' => '../tpl/' . $_SERVER['SERVER_NAME'] . '/').

Blocks can be nested inside blocks.

Requires PHP version 8.2+

Setup

See Example folder for more complex examples

composer install alexkuusk/malli

minimal

require_once '../vendor/autoload.php';

use Alexkuusk\Malli\Malli;

echo (new Malli([
    '_params' => [
        'path' => '../tpl/',
        'file' => 'index.tpl',
        'block' => 'index', //if omitted, file name without extension is used
    ],
    '_data' => [
        'title' => 'Template test',
        'books' => [
            'One',
            'two',
        ]
    ]));

index.tpl

{{ BLOCK:index }}
<html>
<head>
    <title>{{ $title }}</title>
</head>
<body>
<table border=1>
{{ FOREACH $books as $k => $title }}
    <tr>
        <td>{{ $k + 1 }}</td>
        <td>{{ htmlspecialchars($title) }}</td>
    </tr>
{{ /FOREACH }}
</table>
</body>
</html>
{{ /BLOCK:index }}