tjlytle / render
为基于 PHP 模板的视图提供了一个非常简单的渲染。
v1.0.0
2014-02-22 21:31 UTC
Requires (Dev)
- phpunit/phpunit: 3.*
This package is auto-updated.
Last update: 2024-09-26 04:47:16 UTC
README
Render
为基于 PHP 模板的视图提供了一个非常简单的渲染。 这不是 一种模板语言,而是一个简单的包装器,用于处理未定义的模板变量、辅助函数,并将 phtml 文件转换为字符串。
如果你正在使用某种类型的框架,你可能不应该使用这个。我制作这个是为了粘贴(是的,粘贴,如果你已经启动了 composer,则引入真正的模板库)到现有的遗留项目中,并将经典的 HTML 和内嵌 PHP 转换为更现代的 PHP 渲染模板。
功能
- 设置视图变量
- 设置视图辅助函数
- 在布局中渲染模板
示例
index.php
$render = new Render('layout.phtml');
$render->name = 'Tim';
$render->message = 'This is a secret message';
$render->encode = function($value){
return str_rot13($value);
};
echo $render('template.phtml');
layout.phtml
<html>
<head>Example Layout</head>
<body>
<h1>My Old Site</h1>
<div><?php echo $this->content; ?></div>
</body>
</html>
template.phtml
<p>Hi <?php echo $this->name ?>, welcome back.</p>
<p>Here's your secret message: <?php echo $this->encode($this->message); ?></p>