lucid/template

Templating library

v0.0.1 2016-04-12 19:51 UTC

This package is not auto-updated.

Last update: 2024-11-09 18:48:53 UTC


README

Author Source Code Software License

Build Status Code Coverage HHVM

An extendable templating library for php.

Requirements

php >= 5.6

Installation

$ composer require lucid/template

Getting started

<?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!']);

Partials

Inserts

<html>
    <body>

    <div id="container">
        $view->insert('partials/footer.php');
        $view->insert('partials/content.php');
        $view->insert('partials/footer.php');
    </div>

    </body>
</html>

Extending existing templates

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>

Sections

Tempalte Listeners

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`
	}
}