iantch / wp-hbs-engine
在您的WordPress主题/插件中渲染Handlebars模板
0.0.1
2018-02-23 23:56 UTC
Requires
- composer/installers: ~1.0
- zordius/lightncandy: 1.2.0
This package is not auto-updated.
Last update: 2024-09-29 06:14:16 UTC
README
在您的WordPress主题/插件中渲染Handlebars/Moustache模板。Handlebars渲染由LightnCandy提供支持。已测试与WordPress 5.2.3兼容
安装
您可以通过两种方式安装此(必须使用)插件
通过Composer
如果您通过Composer加载依赖项,则可以使用以下命令加载此插件
$ php composer require iantsch/wp-hbs-engine
通过下载
下载/分支此存储库,并将插件文件夹复制到 wp-content/plugins/
。如果您访问 wp-admin
区域中的插件部分,激活它,就可以使用了。
故障排除
您需要通过composer加载zordius/lightncandy
依赖项。安装composer,然后在wp-content/plugins/wp-hbs-engine
文件夹中运行$ composer install
。
插件需要一个具有写入权限的文件夹来缓存Handlebars文件。它尝试创建文件夹wp-content/plugins/wp-hbs-engine/cache
。
插件将尝试从默认文件夹src/templates/partials
中加载部分文件,该文件夹位于您当前的主题中。如果文件夹不存在,请添加它(或使用过滤器钩子来调整)。
用法
在您的functions.php
或插件中设置和修改渲染引擎以满足您的需求
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; });
在您的主题中像调用WordPress函数一样调用它。
global $post; while (have_posts()){ the_post(); $data = (array) $post; the_hbs_template('article', $data); }
在您的article.hbs
中,它使用handlebarsjs.com语法
<h2>{{this.post_title}}</h2> <div>{{{content}}}</div> <a href="{{permalink}}">{{__ 'Click'}}</a>
API
get_hbs_template
the_hbs_template
钩子
有几个钩子可以修改和调整渲染引擎