giggleboxstudios / dispatch-handlebars
dispatch 微型框架的 Handlebars 支持
Requires
- php: >= 5.4.0
- dispatch/dispatch: >= 2.6.2
- xamin/handlebars.php: dev-master
This package is not auto-updated.
Last update: 2022-02-01 12:30:34 UTC
README
这是 Dispatch PHP 微型框架的一个扩展,提供了对 Handlebars 模板的支持。
要求
{ "require": { "php": ">= 5.4.0", "dispatch/dispatch": ">= 2.6.2", "xamin/handlebars.php": "dev-master" } }
安装
此仓库假设您知道如何通过 Composer 安装依赖项
composer.json
{ "require": { "php": ">= 5.4.0", "dispatch/dispatch": ">= 2.6.2", "xamin/handlebars.php": "dev-master", "gigglebox/dispatch-handlebars": "dev-master" } }
在您的当前工作目录中打开一个新终端,并执行 composer install
或 composer update
。
配置
您的应用程序主文件.php
// main layout template file; must contain `{{{content}}}` in <body> config('handlebars.layout') = 'layout'; // set location of .handlbars templates (views and partials) config('handlebars.views') = 'path/to/tempaltes'; // set character encoding for template files; defaults to `UTF-8` config('handlebars.charset') = 'utf-8'; // prefix string to determine partial files, defaults to `_` (underscore) config('handlebars.partial_prefix') = '_'; // [associative array](https://php.ac.cn/manual/en/language.types.array.php) of tagname and function names config('handlebars.helpers') = array('tagName' => 'callback_function'); // path where compiled templates will be cached (this feature does not work yet) config('handlebars.cache') = 'path/to/cached/tempaltes';
注意:如果您没有定义
handlebars.layout
和handlebars.views
,handlebars 将使用默认的dispatch.layout
和dispatch.views
值。
在 Dispatch 中使用 Handlebars
layout.handlebars
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>{{#capitalize page.title }}</title> </head> <body> {{{ content }}} </body> </html>
homepage.handlebars
<!-- homepage.handlebars --> <h1>Hello there, {{ user.notLoggedIn.name }}!</h1>
在我们的示例 index.php
文件中,我们将使用 handlebars()
函数在我们的路由中渲染模板。
index.php
<?php require "vendor/autoload.php"; require "app/functions.php"; // // INIT MODEL // $model = compile_model(); // // CONFIGURE DISPATCH // config(array( 'dispatch.url' => 'https:///appname' , 'handlebars.views' => 'app/templates' , 'handlebars.layout' => 'layout' , 'handlebars.partial_prefix' => '_' , 'handlebars.helpers' => [ 'capitalize' => 'handlebars_capitalize' , 'upper' => 'handlebars_upper' , 'lower' => 'handlebars_lower' ] )); // // EXTEND HANDLEBARS // function handlebars_capitalize($template, $context, $args, $source) { return ucwords($context->get($args)); } function handlebars_upper($template, $context, $args, $source) { return strtoupper($context->get($args)); } function handlebars_lower($template, $context, $args, $source) { return strtolower($context->get($args)); } // // DEFINE ROUTES // on('GET', '/', function () { global $model; handlebars('homepage', $model); }); // // RUN THE APPLICATION // dispatch(); ?>
handlebars()
函数接受三个参数
handlebars();
上述演示的结果 HTML 将渲染成这样
<!doctype html> <html> <head><title>Handlebars Hello World App</title></head> <body> <!-- homepage.handlebars --> <h1>Hello there, stranger!</h1> </body> </html>
扩展 Handlebars 以使用辅助函数
为了为 Handlebars 定义辅助函数,您需要定义您希望使用的函数,并将它们作为数组传递给上面定义的 handlebars.helpers
。
示例
config('handlebars.helpers') = array( 'capitalize' => 'handlebars_capitalize' , 'upper' => 'handlebars_upper' , 'lower' => 'handlebars_lower' ); // extend handlebars function handlebars_capitalize($template, $context, $args, $source) { return ucwords($context->get($args)); } function handlebars_upper($template, $context, $args, $source) { return strtoupper($context->get($args)); } function handlebars_lower($template, $context, $args, $source) { return strtolower($context->get($args)); }
在 handlebars.helpers
中,key
是在您的 Handlebars 模板中使用的标签名,而 value
是当定义此辅助函数时我们想要使用的回调函数的名称。
有关定义辅助函数的更多信息,请参阅 mardix/Handlebars
鸣谢
本包由 Brandtley McMinn 编写,主要基于由 Jesus A. Domingo 编写的 Dispatch-Mustache 包,该包是 Dispatch PHP 微框架的附加组件。
它依赖于 Handlebars PHP 库,该库由 fzerorubigd 和 Behrooz Shabani(又名 everplays)共同编写。