eclipxe/engineworks-templates
Composer 安装命令:
composer require eclipxe/engineworks-templates
包简介
PHP Templates with plugins
README 文档
README
This library is just for running PHP Templates. Similar projects: Slim/Php-View
PHP is a powerfull template engine by itself, you might not need a template library as Twig, Plates or Smarty. It depends on the problem you are facing, maybe you are working with a legacy system, maybe you just don't want it.
Installation
Use composer to install this library composer require eclipxe/engineworks-templates
Basic use
<?php use EngineWorks\Templates\Callables; use EngineWorks\Templates\Plugins; use EngineWorks\Templates\Resolver; use EngineWorks\Templates\Templates; // create callables $callables = new Callables(); $callables->attach( new Plugins\HtmlEscape(), new Plugins\FormatNumber(), new Plugins\Transliterate(), ); // create resolver $resolver = new Resolver(__DIR__ . '/templates'); // create templates $templates = new Templates($callables, $resolver); // fetch the content of a template (templates/user-details.php) /* @var $user array */ $content = $templates->fetch('user-details', ['user' => $user]); // do whatever with the response, I will just echo it echo $content;
About the objects
EngineWorks\Templates\Templates
This class works as a factory of Template objects, it helps to locate this objects with a common Callables object,
in a common directory with a common file extension.
The most common used method would be fetch. It simply creates a Template using the file specified by
directory plus name + extension, with the default callables.
Then will call fetch on that template.
EngineWorks\Templates\Template
This is the main class of the library, it can be created stand alone
or by Templates::create (non-static call).
EngineWorks\Templates\Template::fetch
fetch method receives two arguments, a template name and a variables array.
It will resolve the file name using the Resolver object.
It will convert the array to variables (using extract) in order to make accesible these variables to the file.
Inside the template
The template file is a PHP file, it will have all the variables that were set to fetch method.
Also, you can use $this, wich is refered to the EngineWorks\Templates\Template object.
The $this object offer some functions registered in the Callables object, in the example above the $callables
was attatched with the following functions:
e($string, $this->getDefaultHtmlFlags()): escape as htmljs($string): escape as javascriptejs($string): escape as html and then as javascripturi($string): escape as uri (seerawurlencode)url($url, $vars): create an url with the defined$varsqry($vars): create a query string with the defined$varsfn($number, $decimals = $this->getDefaultDecimals()): return a formatted numbertr($message, $arguments, $encoder = $this->getDefaultEncoder()): return a transliterated message, very useful for inline templates, likehello {name}, I sent you an email to {email}
So, you can use those functions using $this inside your template.
Also, you can use the fetch method to retrieve the content of another template.
This is a template example templates/users-list.php:
<?php /** * @var $pagename string * @var $users array */ ?> <h1><?=$this->e($pagename)?></h1> <ul> <?php foreach ($users as $user): ?> <li>User: <b><?=$this->tr('{fullname} ({nickname})', $user)?></b></li> <?php endforeach; ?> </ul>
This is the templates call:
<?php /* @var $templates \EngineWorks\Templates\Templates */ $templates->fetch('users-list', [ 'pagename' => 'List of users & members', 'users' => [ ['fullname' => 'John Doe', 'nickname' => 'jdoe'], ['fullname' => 'Carlos C Soto', 'nickname' => 'eclipxe'], ], ]);
This would be the result:
<h1>List of users & members</h1> <ul> <li>User: <b>John Doe (jdoe)</b></li> <li>User: <b>Carlos C Soto (eclipxe)</b></li> </ul>
Integrations
Integrate with PSR-7
In order to integrate with a PSR-7 compatible library you can use the method render.
This method is act as a decorator to fetch the template and write the contents into
the ResponseInterface object.
You only need to use this method in case you are using a PSR-7 compatible library.
Otherwise, I recommend you to use fetch method.
As this is optional, the psr/http-message package is not a composer dependence.
Integrate with Slim 4
To use this library in Slim 4 we provide a plugin named Slim4Plugin that offers two methods:
pathFor: shortcut for\Slim\Interfaces\RouteParserInterface::urlFormethodbaseUrl: returnbaseUrlproperty (setup from\Slim\App::getBasePath)
This is a common code to attach the plugin into the Callables collection:
<?php /* @var $callables \EngineWorks\Templates\Callables */ /* @var $app Slim\App */ $callables->attach(new \EngineWorks\Templates\Slim\Slim4Plugin( $app->getRouteCollector()->getRouteParser(), $app->getBasePath() ));
Compatibility
Version 2.x was compatible with PHP 5.4 or higher. It's no longer maintained. Version 3.x is compatible with PHP 7.3 or higher.
Contributing
Contributions are welcome! Please read CONTRIBUTING for details and don't forget to take a look on the TODO and CHANGELOG files.
Copyright and License
The EngineWorks\Templates library is copyright © Carlos C Soto
and licensed for use under the MIT License (MIT). Please see LICENSE for more information.
eclipxe/engineworks-templates 适用场景与选型建议
eclipxe/engineworks-templates 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 59 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 09 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「templates」 「php templates」 「native templates」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 eclipxe/engineworks-templates 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 eclipxe/engineworks-templates 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 eclipxe/engineworks-templates 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
CakePHP 4.x AdminLTE Theme.
Bookdown.io With Bootswatch Styles And Prism Syntax Highlighting
CakePHP 3.x Gentelella Theme.
Native ping command without any dependencies. Through ICMP protocol.
Extremely simple and standard compliant view implementation of MVC pattern
统计信息
- 总下载量: 59
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-09-26