spin/template
Composer 安装命令:
composer require spin/template
包简介
An Active Record like pattern for template rendering with PHP - supporting Twig, Smarty, Plates and Dwoo
README 文档
README
An Active Record like pattern for template rendering with PHP.
- Define and retrieve variables
- Accessor and mutator functions
- Render in any engine
- Built in support for Twig, Smarty, Plates and Dwoo
Installation
Install Spin via composer. Then extend Spin\Template\Template.
composer require spin/template
Example
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; ?>
Output:
Hello John today's date is 2015-08-05. The weather outside is frightful.
As you can see we assign variables to our class either via properties or array syntax and call the render() method, which passes those variables to our template defined in $file and return's the rendered template.
Accessors and mutators
Accessors and mutators allow you to format template variables when retrieving them for rendering or setting their value.
If you have ever used Eloquent mutators for getting/setting data on an object it's the same concept (in fact it's almost the same code - thanks Laravel).
To define an accessor, create a getFooAttribute method in your class where Foo is the camel cased name of the variable you wish to access. See the getFirstNameAttribute method in the example above.
To define a mutator, create a setFooAttribute method in your class where Foo is the camel cased name of the variable you wish to change. See the setFirstNameAttribute method in the example above.
Rendering engine
To use a different rendering engine pass an object that implements Spin\Template\Engine\EngineInterface to the setEngine method.
Support for Twig, Smarty, Plates and Dwoo are already included and can be implemented as per the examples below:
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)); } }
About
Requirements
- Spin works with PHP 5.4 or above (excluding the requirements for any template engine you use)
Submitting bugs and feature requests
Bugs and feature request are tracked on GitHub.
License
Spin is licensed under the MIT License - see the LICENSE.md file for details.
Acknowledgements
This library is heavily inspired by Laravel's Eloquent model class. Thanks guys!
spin/template 适用场景与选型建议
spin/template 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20 次下载、GitHub Stars 达 3, 最近一次更新时间为 2015 年 08 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「twig」 「template」 「smarty」 「view」 「activerecord」 「plates」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 spin/template 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 spin/template 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 spin/template 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
This Symfony bundle integrates PhpSpreadsheet into Symfony using Twig.
A Twig extension to insert css as inline styles with a tag
Caching and compression for Twig assets (JavaScript and CSS).
Rewritten Smarty foreach variant that was invented for use in xoops, but nowadays is used in some other PHP based CMS'es
Twig extensions for Tracy Debugger
统计信息
- 总下载量: 20
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-08-07