unprefix/template-loader
Composer 安装命令:
composer require unprefix/template-loader
包简介
A simple hookable template loader for WordPress. Allow you to load templates in chain from child to plugin.
README 文档
README
WordPress Template Loader
A simple hookable template loader for WordPress. Allow you to load templates in chain from child to plugin.
This is not for the WordPress templates in the form of "name-slug.php" with fallback to name.php. Indeed this is a loader to build a basic data injection for views (templates).
Requirements
Php >= 5.6.x
Examples
The TemplateLoader\Loader class make use of the Fluent interface, so it's possible to concatenate
the calls to ask the instance to do the things.
$loader = new TemplateLoader\Loader('template_slug', new TemplateLoader\DataStorage()); $loader->withData(new ModelInterface()) ->usingTemplate('/relative/file/path.php') ->render()
The class make use of WordPress function locate_template to locate the template file within the child and parent theme.
If you use the library within a plugin it's possible to define a fallback template part file path. As the name fallback says the file will be loaded only in case nothing is found into the previous locations.
$loader = new TemplateLoader\Loader('template_slug', new TemplateLoader\DataStorage()); $loader->withData(new ModelInterface()) ->usingTemplate('/relative/file/path.php') ->butFallbackToTemplate('/plugin/relative/file/path.php') ->render();
Since WordPress function locate_template allow to pass an array along a string, we can do the same by
passing an array of template paths to the method usingTemplate.
Data Type
The data type used to inject values into the template is a class named TemplateLoader\ModelInterface.
ModelInterface doesn't declare any method. It's just a way to ensure the correct type of data is passed into the template loader.
This way we can extends the interface to create our own contracts based on the specific view.
Hooks
The render method, perform some filters that allow third party code to hook into the data and template
to be modified before the template file is loaded.
There are two filter: tmploader_template_engine_data that is generic and pass the $data value and the slug property.
add_filter('tmploader_template_engine_data', function(TemplateLoader\ModelInterface $data, string $slug) { switch($slug) { case 'my_slug': // Do something break; default: break; } return $data; });
The second one is similar but the filter name include the slug template: "tmploader_template_engine_data_{$this->slug}".
This in case you don't want or need to write conditional statements to know which is the current processing template.
add_filter('tmploader_template_engine_data_my_template', function(TemplateLoader\ModelInterface $data) { return new DataTemplate(); // An new instance of a class implementing TemplateLoader\ModelInterface. });
Performances
Since it's usually to call the same view in different portion of the same page, so whitin the same http request, to prevent to access multiple time to the file system only to know where the file template is located, we defined an internal collection that can be used to store the template file paths.
The second time we try to ask the same template, we'll not perform any additional filesystem access in order to load the template file. This improve speed for multiple calls. Also since we don't create a strict relation betwee the template and the data to inject, we can pass every time a different data value.
So, just to clarify with an example, during the same call we can instantiate the loader once and than ask to load the same template multiple times with different data values.
$loader = new TemplateLoader\Loader('template_slug', new TemplateLoader\DataStorage()); $loader->withData(new ModelInterface()) ->usingTemplate('/relative/file/path.php') ->butFallbackToTemplate('/plugin/relative/file/path.php') ->render(); // Some code ... and then ... $loader->withData(new ModelInterface()) ->render();
The second time we call the render method we had changed only the data used within the template since
we have stored the template path related with the slug of the template. In this case template_slug.
Avoiding unnecessary filesystem access that we know are slows.
unprefix/template-loader 适用场景与选型建议
unprefix/template-loader 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 179 次下载、GitHub Stars 达 2, 最近一次更新时间为 2017 年 05 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 unprefix/template-loader 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 unprefix/template-loader 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 179
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 9
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-only
- 更新时间: 2017-05-11