romanzaycev / tooolooop
PHP7轻量级原生模板引擎
0.5.3
2022-01-26 16:10 UTC
Requires
- php: >=7.2.0
- psr/container: ^1.0
Requires (Dev)
- mockery/mockery: ^1
- phpunit/phpunit: ^8
README
PHP7轻量级模板。
安装
通过 Composer
composer require romanzaycev/tooolooop
用法
<?php declare(strict_types = 1); require "vendor/autoload.php"; use Romanzaycev\Tooolooop\Engine; $engine = new Engine(__DIR__ . '/views'); $template = $engine->make('page'); $template->assign(['text' => 'Lorem ipsum']); echo $template->render();
views/page.php
:
<?php $this->extend('layout') ?> <section> <?=$this->e($text)?> </section> <?php $this->start('footer') ?> <footer> Some footer content. </footer> <?php $this->end() ?>
views/layout.php
:
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Example</title> </head> <body> <main> <h1>Example</h1> <?=$this->block('content')?> </main> <?=$this->block('footer')?> </body> </html>
需要更多示例?
要求
PHP >= 7.2.0
扩展库
PSR-11 容器支持
您可以使用 PSR-11 兼容的容器,并将依赖项注入到库内部生成的对象(范围)中。
<?php use Romanzaycev\Tooolooop\Engine; use Psr\Container\ContainerInterface; /** @var ContainerInterface $container */ $container = ...; // Initialize PSR-11 container // and define implementation of Romanzaycev\Tooolooop\Scope\ScopeInterface $engine = new Engine(__DIR__ . '/views'); $engine->setContainer($container); $template = $engine->make('page'); // <-- Scope in this template will be obtained from container
您可以在容器配置中定义 Romanzaycev\Tooolooop\Scope\ScopeInterface
的实现,并通过它来设置引擎实例的范围。
⚠️ 注意!确保容器每次都返回一个新的范围实例!缓存的范围实例不可用。
用户范围
否则,您可以通过 $engine->setScopeClass()
指定类的自定义实现。
<?php use Romanzaycev\Tooolooop\Engine; use Romanzaycev\Tooolooop\Scope\Scope; use Romanzaycev\Tooolooop\Scope\ScopeInterface; class UserSpecificScope extends Scope implements ScopeInterface { // Implement your additions, ex. widget system :-) } $engine = new Engine(__DIR__ . '/views'); $engine->setScopeClass(UserSpecificScope::class);
测试
composer run test