spin / template
一个用于 PHP 模板渲染的 Active Record 样式模式 - 支持 Twig、Smarty、Plates 和 Dwoo
Requires
- php: >=5.4.0
- illuminate/support: ~5.0
Requires (Dev)
- dwoo/dwoo: 2.0b1
- league/plates: ~3.1
- phpunit/phpunit: ~4.0
- smarty/smarty: ~3.1
- twig/twig: ~1.0
This package is auto-updated.
Last update: 2024-09-13 00:24:50 UTC
README
一个用于 PHP 模板渲染的 Active Record 样式模式。
- 定义和检索变量
- 访问器和修改器函数
- 在任何引擎中渲染
- 内置对 Twig、Smarty、Plates 和 Dwoo 的支持
安装
通过 composer 安装 Spin。然后扩展 Spin\Template\Template
。
composer require spin/template
示例
class Homepage extends Spin\Template\Template { /** * The template file */ protected $file = 'templates/home.php'; /** * Convert the first character of first name to uppercase on retrieval */ public function getFirstNameAttribute($value) { return ucfirst($value); } /** * Convert the whole string to lowercase when setting first name */ public function setFirstNameAttribute($value) { $this->attributes['first_name'] = strtolower($value); } /** * Convert the unix timestamp to a human friendly date on retrieval */ public function getDateAttribute($value) { return date('Y-m-d', $value); } } $template = new Homepage(); // Assign variables as properties $template->first_name = 'john'; $template->date = time(); // You can also assign variables as an array $template['weather'] = 'frightful'; // Render the template echo $template->render();
templates/home.php
Hello <?php echo $first_name; ?> today's date is <?php echo $date; ?>. The weather outside is <?php echo $weather; ?>
输出
Hello John today's date is 2015-08-05. The weather outside is frightful.
如你所见,我们通过属性或数组语法将变量分配给我们的类,并调用 render()
方法,该方法将变量传递给我们在 $file
中定义的模板,并返回渲染的模板。
访问器和修改器
访问器和修改器允许你在渲染时格式化模板变量,或者设置它们的值。
如果你曾经使用过 Eloquent 修改器 来获取/设置对象上的数据,那么这个概念是相同的(实际上代码几乎相同 - 感谢 Laravel)。
要定义一个访问器,在你的类中创建一个 getFooAttribute
方法,其中 Foo
是你想访问的变量的驼峰式名称。请参见上面的示例中的 getFirstNameAttribute
方法。
要定义一个修改器,在你的类中创建一个 setFooAttribute
方法,其中 Foo
是你想改变的变量的驼峰式名称。请参见上面的示例中的 setFirstNameAttribute
方法。
渲染引擎
要使用不同的渲染引擎,将实现 Spin\Template\Engine\EngineInterface
的对象传递给 setEngine
方法。
已经包含了对 Twig、Smarty、Plates 和 Dwoo 的支持,并且可以根据下面的示例实现
Twig
class Homepage extends Spin\Template\Template { protected $file = 'home.twig'; public function __construct() { $loader = new Twig_Loader_Filesystem('/path/to/templates'); $twig = new Twig_Environment($loader); $this->setEngine(new Spin\Template\Engine\TwigEngine($twig)); } }
Smarty
class Homepage extends Spin\Template\Template { protected $file = 'home.smarty'; public function __construct() { $smarty = new Smarty(); $smarty->setTemplateDir('/path/to/templates'); $smarty->setCompileDir('/path/to/templates_c'); $this->setEngine(new Spin\Template\Engine\SmartyEngine($smarty)); } }
Plates
class Homepage extends Spin\Template\Template { protected $file = 'home'; public function __construct() { $plates = new League\Plates\Engine('/path/to/templates'); $this->setEngine(new Spin\Template\Engine\PlatesEngine($plates)); } }
Dwoo
class Homepage extends Spin\Template\Template { protected $file = 'home.dwoo'; public function __construct() { $dwoo = new Dwoo\Core(); $dwoo->setTemplateDir('/path/to/templates'); $dwoo->setCompileDir('/path/to/templates_c'); $this->setEngine(new Spin\Template\Engine\DwooEngine($dwoo)); } }
关于
要求
- Spin 与 PHP 5.4 或更高版本(不包括你使用的任何模板引擎的要求)兼容
提交错误和功能请求
错误和功能请求在 GitHub 上跟踪。
许可证
Spin 在 MIT 许可证下发布 - 有关详细信息,请参阅 LICENSE.md
文件。
致谢
这个库在很大程度上受到 Laravel 的 Eloquent 模型类的启发。谢谢大家!