bfitech/zaplate

一个伪模板引擎。

1.4.1 2020-02-05 14:44 UTC

This package is auto-updated.

Last update: 2024-09-06 01:00:42 UTC


README

PHP 是一个 HTML 模板引擎。这仅仅略微简化了它,足以渲染大部分静态 HTML,这正是 zap* 所需要的,因为它面向 RESTful。

使用其他完整的模板引擎来渲染复杂的动态 HTML。

Latest Stable Version Latest Unstable Version Build Status Codecov GitHub license

安装

从 Packagist 安装它

$ composer -vvv require bfitech/zaplate

示例用法

template.php

<p><?php echo $group ?></p>
<ul>
	<?php foreach ($members as $member): ?>
	<li><?php echo $member ?></li>
	<?php endforeach; ?>
</ul>

renderer.php

<?php

require __DIR__ . '/vendor/autoload.php';

class Filter {
	public function whoami($name) {
		if (is_string($name))
			return $name;
		return array_map(function($iname){
			if (stripos($iname, 'jekyll') !== false)
				return 'Mr Hyde';
			return $iname;
		}, $name);
	}
}

BFITech\ZapTemplate\Template::load('template.php', [
	'group' => "Extraordinary Gents",
	'members' => [
		'Allan Quatermain',
		'Henry Jekyll',
	],
], [
	[(new Filter), 'whoami'],
]);

运行它

$ php renderer.php
<p>Extraordinary Gents</p>
<ul>
	<li>Allan Quatermain</li>
	<li>Mr Hyde</li>
</ul>

小贴士:如果您想在生产中压缩 HTML,请在渲染之前通过 您的我最喜欢的 压缩器 运行模板文件,例如。

$ [ ! -f template.orig.php ] && cp template.{,orig.}php
$ php -w template.orig.php | \
> html-minifier \
>   --collapse-whitespace \
>   --trim-custom-fragments > \
> template.php

... 嘿!

$ php renderer.php
<p>Extraordinary Gents</p><ul><li>Allan Quatermain</li><li>Mr Hyde</li></ul>

文档

使用以下方式可获取文档

$ doxygen
$ x-www-browser docs/html/index.html