template-interop/middleware

PSR-15 HTTP 中间件,用于 template-interop/engine

0.0.2 2019-09-17 19:51 UTC

This package is auto-updated.

Last update: 2024-09-18 07:36:07 UTC


README

PSR-15 中间件,借助任何模板引擎构建响应视图。

兼容引擎

  • Twig
  • Latte
  • Mustache
  • Smarty
  • Plates
  • Blade
  • Dwoo
  • Div
  • Foil
  • Stamp

要求

安装

此包可通过 Composer 以 template-interop/middleware 的形式安装和自动加载。

composer require template-interop/middleware

用法

$dispatcher = new Dispatcher([
    new TemplateEngine($twig, $streamFactory),
    new Router([
        'GET /hello/{name}' => function(ServerRequestInterface $request) {
            return (new Response(200))
                ->withAttribute('template-name', 'hello')
                ->withAttribute('template-parameters', ['name' => $request->getAttribute('name')])
            ;        
        }   
    ])
]);
$response = $dispatcher->dispatch(new ServerRequest);

请求属性名称可配置

<?php

use Interop\Template\Middleware\TemplateEngine;
use Psr\Http\Message\ServerRequestInterface;

new TemplateEngine($twig, $streamFactory, 'tpl_name', 'tpl_params');

包含以下选项来配置 json_decode 函数