template-interop/middleware

PSR-15 HTTP middleware for template-interop/engine

0.0.2 2019-09-17 19:51 UTC

This package is auto-updated.

Last update: 2024-11-18 07:55:51 UTC


README

PSR-15 middleware to build a view for a response thanks to any template engine.

Compatible engines:

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

Requirements

Installation

This package is installable and autoloadable via Composer as template-interop/middleware.

composer require template-interop/middleware

Usage

$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);

Request attributes names are configurable:

<?php

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

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

Contains the following options to configure the json_decode function: