jyoungblood/slime-render

PHP 抽象函数,帮助在 Slim 框架中使用 Handlebars (LightnCandy) 更容易地渲染视图。

1.3.3 2024-09-07 20:00 UTC

This package is auto-updated.

Last update: 2024-09-07 20:03:23 UTC


README

PHP 抽象函数,帮助在 Slim 框架 (v4) 中使用纯文本、HTML、JSON 和 Handlebars (使用 LightnCandy) 更容易地渲染视图。

这些函数旨在提供一个简化和标准化的接口,用于将各种类型的数据驱动响应作为 PSR-7 对象渲染,以便与 Slim 一起使用。

包括用于 Slim 应用的 Slime 模板

安装

使用 composer 轻松安装

composer require jyoungblood/slime-render
use Slime\render;
require __DIR__ . '/vendor/autoload.php';

要求

用法

render::html($request, $response, $string, $status = 200)

将字符串渲染为 HTML。返回一个标准 Slim (PSR-7) 响应对象,带有可选的 HTTP 状态码(默认为 200)。

$app->get('/', function ($req, $res, $args) {

  return render::html($req, $res, '<h2>Hey whats up</h2>');

});

此外,还可以指定一个 HTML 文件的路径以加载和渲染,而不是字符串

$app->get('/', function ($req, $res, $args) {

  return render::html($req, $res, '/hey/whats-up.html');

});

render::text($request, $response, $string, $status = 200)

将字符串渲染为纯文本。返回一个标准 Slim (PSR-7) 响应对象,带有可选的 HTTP 状态码(默认为 200)。

$app->get('/', function ($req, $res, $args) {

  return render::text($req, $res, 'Hey whats up');

});

render::hbs($request, $response, $parameters, $status = 200)

使用数据数组(包括任何部分和全局 locals 变量数组)渲染特定的 Handlebars 模板。返回一个标准 Slim (PSR-7) 响应对象,带有可选的 HTTP 状态码(默认为 200)。

$app->get('/', function ($req, $res, $args) {

  return render::hbs($req, $res, [
    'template' => 'index',
    'layout' => '_layouts/base', // optional "wrapper" layout template
    'title' => 'Page title', // for HTML <title> tag
    'data' => [
      'name' => 'Ringo',
      'friends' => [
        'Paul', 'George', 'John'
      ]
    ],
  ], 200); // optional status code, 200 by default

});

解析函数期望模板位于具有 html 文件扩展名的 templates 目录中。这可以通过在全局 settings 数组中定义这些变量来自定义

$GLOBALS['settings']['templates']['path'] = 'pages';
$GLOBALS['settings']['templates']['extension'] = 'hbs';

此外,还可以添加一个 locals 数组,以便在所有模板中可用变量

$GLOBALS['locals'] = [
  'year' => date('Y'),
  'site_title' => 'Web Site Title',
  'site_code' => 'WST',
  'site_domain' => 'example.com',
];
Welcome to {{locals.site_title}}, the year is {{locals.year}}!

PHP $_GET 和 $_POST 变量的参数自动提供给使用此函数渲染的模板,使用变量 {{GET}}{{POST}}

<!-- assuming a url like /hello/?name=Delilah&location=New%20York%20City -->
Hey there, {{GET.name}}, what's it like in {{GET.location}}?

查看 Handlebars 烹饪书,了解您可以使用 LightnCandy 和 Handlebars 做的所有事情。

此外,我们还包含了一些辅助函数。

date 辅助函数将 PHP date() 函数应用于给定的变量或字符串(或 now 关键字表示当前时间)

Date from unix timestamp: {{date unix_ts_var "d/m/Y"}}
Current date: {{date "now" "d/m/Y"}} <!-- use the "now" keyword instead of a variable to use the current time -->
Date from non-unix timestamp: {{date non_unix_ts_var "d/m/Y" "convert"}} <!-- add the "convert" parameter to convert the variable to unix time using strtotime() -->

#is 块辅助函数允许基本条件逻辑

Is it 1981? {{#is locals.year "==" "1981"}} Yes! {{else}} No! {{/is}}

自定义辅助函数很容易创建。请参阅这些辅助函数在 initialize_handlebars_helpers() 中的定义。Handlebars 烹饪书还包括创建 自定义辅助函数自定义块辅助函数 的参考。

render::handlebars($parameters)

render::hbs() 一样,使用数据数组渲染特定的 Handlebars 模板,但返回原始 HTML 而不是 PSR-7 响应。

$app->get('/', function ($req, $res, $args) {

  echo render::handlebars([
    'template' => 'email/test',
    'data' => [
      'link' => 'https://jy.hxgf.io',
    ]
  ]);

  return $res;
});

render::redirect($request, $response, $string, $status = 302)

将重定向作为带有可选 HTTP 状态码的标准 Slim (PSR-7) 响应对象渲染。

  return render::redirect($req, $res, 'https://google.com/');

render::json($request, $response, $data, $status = 200)

将数组或数据渲染为标准 Slim (PSR-7) 响应对象,内容类型为 application/json,并可选设置 HTTP 状态码。

$app->get('/json/', function ($req, $res, $args) {

  $data = [
    'name' => 'Ringo',
    'friends' => [
      'Paul', 'George', 'John'
    ]
  ];

  return render::json($req, $res, $data);

});

render::lightncandy_html($parameters)($data)

准备并编译特定的 Handlebars 模板,包括数据数组、任何部分(partials)以及全局的 locals 变量数组。
这会被 render::hbs() 自动调用,但如果需要也可以作为独立函数使用。

$args = [
  'template' => 'index',
  'layout' => '_layouts/base',
  'title' => 'Page title',
];

$data = [
  'name' => 'Ringo',
  'friends' => [
    'Paul', 'George', 'John'
  ]
];

echo render::lightncandy_html($args)($data);

render::initialize_handlebars_helpers()

供内部使用于 lightncandy_html()。定义了一些自定义的 Handlebars 辅助函数,供 LightnCandy 编译器使用。