An extendable templating library for php.
php >= 5.6
$ composer require lucid/template
<?php
use Lucid\Template\Engine;
use Lucid\Template\Loader\FilesystemLoader;
$engine = new Engine(new Loader(['path/to/templates']));
$engine->render('partials/content.php', ['title' => 'Hello World!']);
<html>
<body>
<div id="container">
$view->insert('partials/footer.php');
$view->insert('partials/content.php');
$view->insert('partials/footer.php');
</div>
</body>
</html>
The templates
partials/content.php
:
<?= $view->extend('master.php') ?>
<?= $view->section('content') ?>
<p>Extended content</p>
<?= $view->endsection() ?>
master.php
:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title><?= $title ?></title>
</head>
<body>
<div id="main">
<?= $view->section('content') ?>
<p>The default content.</p>
<?= $view->endsection() ?>
</div>
</body>
</html>
Adding template listeners can be usefull if you want to add data to a specific template. This data my be derieved from any resource you may want (e.g. DB, Container, etc).
<?php
$view->addListener('head.php', new RenderHeadListener($headerData));
Your listener may look something like this
<?php
use Lucid\Template\Listener\ListenerInterface;
class RenderHeadListener implements ListenerInterface
{
private $data;
public function __construct(array $headerData)
{
$this->data = $data;
}
public function onRender(TemplateDataInterface $data)
{
// add header data to `$data`
}
}