lucid/template

模板库

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

This package is not auto-updated.

Last update: 2024-09-14 17:59:01 UTC


README

Author Source Code Software License

Build Status Code Coverage HHVM

这是一个可扩展的 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>

扩展现有模板

模板

部分模板/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>

部分

模板监听器

如果您想向特定模板添加数据,添加模板监听器可能很有用。这些数据可能来自任何您可能需要的资源(例如数据库、容器等)。

<?php

$view->addListener('head.php', new RenderHeadListener($headerData));

您的监听器可能看起来像这样

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