bnomei/kirby-handlebars
Composer 安装命令:
composer require bnomei/kirby-handlebars
包简介
Kirby Component for semantic templates with Handlebars and Mustache
关键字:
README 文档
README
Kirby Plugin for semantic templates with Handlebars and Mustache
Installation
- unzip master.zip as folder
site/plugins/kirby-handlebarsor git submodule add https://github.com/bnomei/kirby-handlebars.git site/plugins/kirby-handlebarsorcomposer require bnomei/kirby-handlebars
Templates
You can can have handlebars and php templates in your /site/templates folder. The plugin will be used as template component for all files with hbs extension and default back to Kirbys core template component for the ones written in php.
Partials
Partials are stored at /site/template/partials. Think of them as reusable blocks injected into your handlebars templates before they get compiled.
/site/templates/partials/piece-of-cake.hbs
Piece of {{ cake }}
/site/templates/call-a-partial.hbs
{{> piece-of-cake }}
/content/pizza/call-a-partial.txt
Title: 🍕
/site/controllers/call-a-partial.php
<?php return function ($site, $page, $kirby) { return ['cake' => $page->title()]; // cake => 🍕 };
localhost:80/pizza
Piece of 🍕
Data
For the template to render your content, it needs to know which data to use as variables and list in handlebars. This plugin gives you several ways to provide this data, and you can use them all at once. The data from queries will be merged (recursively) with data from controllers and models.
Built-in Queries
The plugin has a few queries built-in. This allows you to use the queried result directly in your handlebar templates. For example, you can use {{ page.title }} in any handlebar template without having to define that value yourself. You can override these in the bnomei.handlebars.queries option.
<article id="{{ page.slug }}"> <h2>{{ page.title }}</h2> {{ page.text }} </article>
{{ site.title }} {{ site.url }} {{ page.id }} {{ page.title }} {{ page.text }} {{ page.url }} {{ page.slug }} {{ page.template }}
Optional parsing of queries in data strings
By default, plain strings from content, controllers, and models are rendered as literal data. This prevents editable content from executing Kirby Query methods before Handlebars renders.
If all rendered content is trusted, you can re-enable the old behavior:
return [ 'bnomei.handlebars.resolve-content-queries' => true, ];
With this option enabled, strings can contain Kirby Query placeholders:
/content/blog/unesco-heritage/post.txt
Title: Unesco heritage ---- Date: 2017-12-07 ---- Art: twirling ---- Text: On {{ page.date.toDate('d.m.Y') }} the Neapolitan art of pizza {{ page.art }} has joined Unesco’s list of intangible heritage.
/site/templates/post.hbs
<article id="{{ page.slug }}"> <h2>{{ page.title }}</h2> {{ page.text }} </article>
localhost:80/blog/unesco-heritage
<article id="unesco-heritage"> <h2>Unesco heritage</h2> <p>On 07.12.2020 the Neapolitan art of pizza twirling has joined Unesco’s list of intangible heritage.</p> </article>
Controllers
/content/home/home.txt
Title: Home
/site/controllers/home.php
<?php return function ($site, $page, $kirby) { return [ 'c'=> 'Cassia', 'counting' => [ ['label' => 1], ['label' => 2], ['label' => 3], ], ]; };
/site/templates/default.hbs
{{ page.title }} of <i>{{ c }}</i>. <ul> {{# counting }}<li>{{ label }}</li>{{/ counting }} </ul>
Models: Method
/site/models/home.php
<?php class HomePage extends Page { public function handlebarsData(): array { return [ 'c'=> 'Cassia', 'counting' => [ ['label' => 1], ['label' => 2], ['label' => 3], ], ]; } }
Models: Extend model from Plugin and define exported methods
<?php class HomePage extends \Bnomei\HandlebarsPage { // declare what public methods to export public static $handlebarsData = [ 'c', // $this->c() 'counting' // $this->counting() ]; public function c(): string { return 'Cassia'; } public function counting(): array { return [ ['label' => 1], ['label' => 2], ['label' => 3], ]; } }
handlebars()/hbs() helper
Maybe you just need to parse some handlebars when you create your data, or you do not use the template component at all. For the later disable the component with the bnomei.handlebars.component config setting and just use the provided helper functions. The hbs()/handlebars() take the same parameters as the Kirby snippet() function. This means they echo the result by default but take a third function parameter to enforce returning the value.
/site/templates/render-unto.hbs
Render unto {{ c }} the things that are {{ c }}'s, and unto {{ g }} the things that are {{ g }}'s.
/site/templates/non-hbs-template.php
<?php // echo template 'render-unto' // data from site/controllers/home.php merged with custom array hbs('render-unto', ['c'=> 'Caesar', 'g'=>'God']);
Render unto Caesar the things that are Caesar's, and unto God the things that are God's.
// return template 'render-unto' $string = hbs('render-unto', ['c' => 'Cat', 'g' => 'Dog'], true); echo $string;
Render unto Cat the things that are Cat's, and unto Dog the things that are Dog's.
Settings
| bnomei.handlebars. | Default | Description |
|---|---|---|
| component | true |
if false no templating will be handled by this plugin and you need to use the hbs()/handlebars() functions. |
| dir-templates | callback |
returning kirby()->roots()->templates() |
| dir-partials | callback |
returning kirby()->roots()->templates().'/partials' |
| extension-input | hbs |
|
| extension-output | php |
hbs compiled to php |
| queries | [...] |
an array of predefined queries you can use in your templates |
| resolve-content-queries | false |
if true, parse Kirby Query placeholders in plain content/controller/model strings |
Dependencies
Disclaimer
This plugin is provided "as is" with no guarantee. Use it at your own risk and always test it yourself before using it in a production environment. If you find any issues, please create a new issue.
License
It is discouraged to use this plugin in any project that promotes racism, sexism, homophobia, animal abuse, violence or any other form of hate speech.
bnomei/kirby-handlebars 适用场景与选型建议
bnomei/kirby-handlebars 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 10, 最近一次更新时间为 2025 年 10 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「performance」 「cache」 「template」 「mustache」 「controller」 「model」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bnomei/kirby-handlebars 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bnomei/kirby-handlebars 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bnomei/kirby-handlebars 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
g4 application profiler package
repository php library
CSS/Javascript Minificator, Compressor and Concatenator for TYPO3 - highly configurable frontend asset optimization for CSS/JS merging, minification and compression with optional body parsing, async/defer loading, inline output, data-ignore exclusions, SRI integrity validation/calculation, external
WordPress mu-plugin to remove jQuery Migrate from the list of jQuery dependencies and to allow jQuery to enqueue before </body> instead of in the <head>.
Create link to static resources with cache-breaking segment based on md5 of the file
Laravel 5 - Repositories to the database layer
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 32
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-08