gelembjuk/模板

适用于不同PHP模板引擎(如Smarty、Twig)的通用接口。此包允许在不修改PHP代码的情况下在引擎之间切换

1.0.7 2016-04-22 09:15 UTC

This package is not auto-updated.

Last update: 2024-10-02 08:58:28 UTC


README

适用于不同PHP模板引擎(如Smarty、Twig)的通用接口。此包允许在不修改PHP代码的情况下在引擎之间切换。

创建此包是为了能够在不修改应用程序代码的情况下迁移到不同的流行PHP模板引擎。然而,仍然需要修改模板。

安装

使用composer: gelembjuk/模板 require: {"gelembjuk/模板": "1.*"}

此外,您还需要安装您想要使用的模板引擎 - smarty/smarty 或 twig/twig(希望以后能添加对其他引擎的支持)。

配置

配置是通过构造函数选项(作为哈希参数)在运行时完成的

$templateroptions = array(
		'templatepath' => $templatesdir, // directory where templates are stored
		'compiledir' => $compiledir,    // directory to use for compilation temp files. It is needed fro Smarty, but not Twig 
		'usecache' => false,       // don't cache . If true then use cache
		'cachedir' => $cachedir,   // path to cache directory. used if `usecache` is true
		'extension' => 'htm' // our templates files will have htm extension
	);

用法

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

$templater = new Gelembjuk\Templating\SmartyTemplating();
// $templater = new Gelembjuk\Templating\TwigTemplating();

// init template engine
$templater->init($templateroptions);

$templater->setVar('firstname',"John");
$templater->setVar('lastname',"Smith");

$templater->setTemplate('homepage');

// fetch page page content
$pagecontent = $templater->fetchTemplate();

echo $pagecontent;

在Smarty的情况下,文件 homepage.htm

<div>

<h1>Hello {$lastname}, {$firstname} </h1> 

</div>

在Twig的情况下,文件 homepage.htm

<div>

<h1>Hello {{ lastname }}, {{ firstname }} </h1> 

</div>

作者

Roman Gelembjuk (@gelembjuk)