anovsiradj/cutter

灵活的模板库

4.0.0 2024-06-10 01:25 UTC

This package is auto-updated.

Last update: 2024-09-10 02:02:31 UTC


README

灵活的模板库。灵感来自 Blade (Laravel) 和 Twig (Symfony)。

在 PHP 5.6~7~8 上进行了测试。

安装

下载此源代码 或通过 Composer

composer require anovsiradj/cutter

示例

/index.php

require 'Cutter.php'; // directly or composer

$cutter = new anovsiradj\Cutter;
$cutter->set('layout','/layouts/main');

$cutter->data('page_title', 'My Posts'); // set variable

$cutter->view(
	'/pages/home',
	['date_today' => date('Y-m-d')], // set variable(s)
);

/layouts/main.php

使用 section() 来定义部分

<!DOCTYPE html>
<html>
	<head>
		<title><?= $page_title ?></title>
	</head>

	<body>
		<div>
			<?php $cutter->section('content') ?>
		<div>

		<?php $cutter->section('script') ?>
	</body>
</html>

/pages/home.php

使用 begin()end() 来输出缓冲区部分

<?php $this->begin('content') ?>
<ul>
	<li>Post title 1</li>
	<li>Post title 2</li>
	<li>Post title 3</li>
</ul>
<?php $this->end() ?>

<?php $this->begin('script') ?>
<script>alert('date today is <?php echo $date_today ?>')</script>
<?php $this->end() ?>

更多信息,请查看 /example/

参考

类方法

get( $key ) : void;
set( $key, mixed $val ) : void;

data( mixed $any [, mixed $val] ): mixed;

load( $file [, bool $isob = false] ): void;

view( mixed $name [, array $data = [] [, bool $render = true ] ] ) : void;

render( [array $data = [] ] ) : void;

section( $name ) : bool;

begin( $name ) : void;
end(): void

开发

TODO

  • 单文件库 / Phar?
  • 继承
  • 动态路径
  • 动态视图

所有建议都受欢迎。谢谢。

参考