iantsch/wp-hbs-engine
Composer 安装命令:
composer require iantsch/wp-hbs-engine
包简介
Renders Handlebars templates within your WordPress Theme/Plugin
README 文档
README
Renders Handlebars/Moustache templates within your WordPress Theme/Plugin. Handlebars rendering is powered by LightnCandy. Tested with WordPress 5.2.3
Installation
You can install this (must-use) plugin two ways
Via Composer
If you load your dependenies via Composer you can load this plugin with
$ php composer require iantsch/wp-hbs-engine
Via Download
Download/fork this repository and copy the plugin-folder into wp-content/plugins/.
If you visit your Plugin section in the wp-admin area, activate it and you are good to go.
Troubleshoots
You need to load the zordius/lightncandy dependency via composer. Install composer and run $ composer install in the wp-content/plugins/wp-hbs-engine folder.
The plugin needs a folder with write permissions for caching the handlebar files. It tries to create the folder wp-content/plugins/wp-hbs-engine/cache.
The plugin will try to load partials from the default folder src/templates/partials within your current theme. If the folder does not exist please add it (or use the filter hook to adapt).
Usage
Setup and modify the render engine to your needs in your functions.php or your plugin
add_filter('MBT/Engine/Handlebars/Helpers', function($helpers) { $helpers['__'] = function($string) { return __($string, 'mbt'); }; $helpers['permalink'] = 'get_permalink'; $helpers['content'] = function() { return apply_filters('the_content', get_the_content()); }; return $helpers; });
In your theme call it like a WordPress function.
global $post; while (have_posts()){ the_post(); $data = (array) $post; the_hbs_template('article', $data); }
In your article.hbs it is handlebarsjs.com syntax
<h2>{{this.post_title}}</h2> <div>{{{content}}}</div> <a href="{{permalink}}">{{__ 'Click'}}</a>
API
get_hbs_template
| Parameter | Type | Description |
|---|---|---|
$template |
string | template name |
$data |
string | associative array with all the needed data for the template |
$templateDir |
string | boolean | absolutee path to template entry directory, defaults to /src/templates/ in current theme |
$cacheDir |
string | boolean | absolute path to caching directory, defaults to /cache/ in plugins folder |
Returns: $html - the rendered HTML output |
the_hbs_template
| Parameter | Type | Description |
|---|---|---|
$template |
string | template name |
$data |
string | associative array with all the needed data for the template |
$templateDir |
string | boolean | absolutee path to template entry directory, defaults to /src/templates/ in current theme |
$cacheDir |
string | boolean | absolute path to caching directory, defaults to /cache/ in plugins folder |
| echoes the rendered HTML output |
Hooks
There are several hooks to modify and adapt the render engine
Filters
MBT/Engine/Handlebars/Extension
| Parameter | Type | Description |
|---|---|---|
$extension |
string | file extension, default: hbs |
Returns: $extension - file extension of your handlebar templates |
MBT/Engine/Handlebars/Helpers
| Parameter | Type | Description |
|---|---|---|
$helpers |
array | associative array with callback functions for the implemented helpers |
$this |
object | Current instance of Handlebars engine |
Returns: $helpers - array of handlebars helper |
MBT/Engine/Handlebars/Flags
| Parameter | Type | Description |
|---|---|---|
$flags |
int | bitwise flags used by LightnCandy |
$this |
object | Current instance of Handlebars engine |
Returns: $flags - LightnCandy Flags |
MBT/Engine/Handlebars/Partials
| Parameter | Type | Description |
|---|---|---|
$partials |
array | associative array with the relative path to the partials |
$this |
object | Current instance of Handlebars engine |
Returns: $partials - array of available partials |
MBT/Engine/Handlebars/DefaultData
| Parameter | Type | Description |
|---|---|---|
$defaultData |
array | fallback data for the template |
$template |
string | template name |
Returns: $defaultData - fallback data for the template |
MBT/Engine/Handlebars/Data
| Parameter | Type | Description |
|---|---|---|
$data |
array | associative array with all the needed data for the template |
$template |
string | template name |
Returns: $data - the data for the template |
MBT/Engine/Handlebars/Html
| Parameter | Type | Description |
|---|---|---|
$html |
string | rendered HTML output (with data) |
$template |
string | template name |
$data |
array | data used to render output |
Returns: $html - the rendered HTML output |
Actions
MBT/Engine/Handlebars/Init
| Parameter | Type | Description |
|---|---|---|
$this |
object | Current instance of Handlebars engine |
Credits
@iantsch - web developer behind this and other projects.
iantsch/wp-hbs-engine 适用场景与选型建议
iantsch/wp-hbs-engine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 35 次下载、GitHub Stars 达 4, 最近一次更新时间为 2018 年 02 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「plugins」 「wordpress」 「handlebars」 「lightncandy」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 iantsch/wp-hbs-engine 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 iantsch/wp-hbs-engine 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 iantsch/wp-hbs-engine 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Handlebars processor for php
This Symfony bundle provides integration for the handlebars template engine.
Parse Handlebars templates to a spec-compliant AST with PHP.
Handlebars asset for yii2
An extremely fast PHP implementation of handlebars ( http://handlebarsjs.com/ ) and mustache ( http://mustache.github.io/ ). This package forked from Zordius/LightnCandy.
A starting point to make working with rad-theme-engine easier
统计信息
- 总下载量: 35
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-02-24