mindplay/kisstpl
Composer 安装命令:
composer require mindplay/kisstpl
包简介
View service / template engine for plain PHP templates
README 文档
README
A very simple view-service / template-engine for plain PHP templates.
I wanted a template engine that uses view-models (objects) rather than view-dictionaries (arrays) as are typical in most PHP template engines.
Oh, and if you don't like typing htmlspecialchars() all day, maybe try this.
The view-service is tied to a root folder and a root namespace:
$service = new ViewService(new SimpleViewFinder('my/app/views', 'app\view')); $hello = new \app\view\HelloWorld(); $service->render($hello); // -> "my/app/views/HelloWorld.view.php"
The render() statement in this example will render the template
my/app/views/HelloWorld.view.php, passing the view-model object to
the rendered template as $view - the SimpleViewFinder is responsible
for locating the actual template based on the type of view-model.
The render() method also accepts a second argument, allowing you to
render different templates for the same view-model:
$service->render($hello, 'boom'); // -> "my/app/views/HelloWorld.boom.php" $service->render($hello, 'bang'); // -> "my/app/views/HelloWorld.bang.php"
You can type-hint in the beginning of a template file for IDE support:
<?php use app\view\HelloWorld; /** * @var HelloWorld $view */ ?> ...
Alternatively, for type-safe template rendering, you can also type-hint statically, by returning a closure:
use app\view\HelloWorld; use mindplay\kisstpl\Renderer; <?php return function(HelloWorld $view, Renderer $renderer) { ?> ... <?php }
Things like layouts can be accomplished by using plain OOP composition.
For example, let's say we have a Layout view-model with a $body
property, and we have an instance of the layout view-model in a $layout
property of the HelloWorld model - to implement a typical two-step
layout, in the HelloWorld template, use begin() and end() to buffer
and capture a section of content:
<?php use app\view\HelloWorld; use mindplay\kisstpl\ViewService; /** * @var HelloWorld $view * @var ViewService $this */ $view->layout->title = 'My Page!'; $this->begin($view->layout->body); ?> <h1>Hello!</h1> <p>Body content goes here...</p> <?php $this->end($view->layout->body); $this->render($view->layout);
Note that begin() and end() take variable references as arguments -
the call to end() will apply the captured content to $view->layout->body.
There is deliberately no view rendering "pipeline", or any concept of layout, and this is "a good thing" - your templates have complete control of the rendering process, you have IDE support all the way,
You can also capture rendered content and return it, instead of sending the rendered content to output:
$content = $service->capture($hello);
You can use this feature to implement "partials", since it can be called
from within a template. Like render(), the capture() method also accepts
a second argument allowing you to render different views of the same view-model.
You can of course also extend ViewService with custom functionality - an
interface Renderer defines the four basic methods, render(), capture(),
begin() and end() so you can type-hint and swap out implementations as needed.
You can also replace the ViewFinder implementation if you need custom logic
(specific to your project) for locating templates. A few implementations are
included:
-
SimpleViewFinderfor direct 1:1 class-to-file mapping (and zero overhead from calls tofile_exists()) with a specified base namespace and root path. -
LocalViewFinderfor direct 1:1 class-to-file mapping (and zero overhead) not limited to any particular namespace, and assuming local view-files located in the same path as the view-model class file. -
DefaultViewFinderwhich searches a list of root-paths and defaults to the first template found. -
MultiViewFinderwhich allows you to aggregate as many otherViewFinderinstances as you need, and try them in order.
The latter is useful in modular scenarios, e.g. using a "theme" folder for template overrides, allowing you to plug in as many conventions for locating views as necessary.
mindplay/kisstpl 适用场景与选型建议
mindplay/kisstpl 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 52.31k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2014 年 10 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mindplay/kisstpl 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mindplay/kisstpl 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 52.31k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 21
- 依赖项目数: 1
- 推荐数: 1
其他信息
- 授权协议: LGPL-3.0
- 更新时间: 2014-10-31