roolith / template-engine
PHP 简单模板引擎
1.0.3
2020-09-01 09:26 UTC
Requires (Dev)
- phpunit/phpunit: ^9.2
This package is auto-updated.
Last update: 2024-08-29 05:31:05 UTC
README
没有过度复杂的东西!模板文件中只有简单的 PHP。没有使用 eval
,它使用输出缓冲!
安装
composer install roolith/template-engine
用法
$view = new \Roolith\Template\Engine\View(__DIR__ . '/views'); try { $data = [ 'content' => 'home content', 'title' => 'home page', ]; echo $view->compile('home', $data); } catch (\Roolith\Template\Engine\Exceptions\Exception $e) { echo $e->getMessage(); }
views
文件夹包含 -
home.php
partials/header.php
partials/footer.php
其中 home.php
<?php $this->inject('partials/header') ?> <p><?= $this->escape('content') ?></p> <?php $this->inject('partials/footer') ?>
header.php
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title><?= $this->escape('title') ?></title> <link rel="stylesheet" href="<?= $this->url('assets/app.css') ?>"> </head> <body>
footer.php
<script src="<?= $this->url('assets/app.js') ?>"></script> </body> </html>
您可以使用 escape
方法或直接以纯文本形式打印变量
<title><?= $this->escape('title') ?></title>
或者
<title><?= $title ?></title>
inject
方法允许注入另一个视图
$this->inject('partials/footer')