View on GitHub

h2l

Example: Use Twig as templating

We simply create a Response subclass that injects any variables into the Twig templates, renders it and sets the rendered output as the Message body.

backend/response/Twig.php

namespace backend\response;

use alkemann\h2l\{ Environment, Message, Response };
use alkemann\h2l\util\Http;
use Twig\Environment as Twig_Environment;
use Twig\Loader\FilesystemLoader;

class Twig extends Response
{

    public function __construct(
        array $data,
        int $code = Http::CODE_OK,
        array $config = [])
    {
        $template = ($config['template'] ?? 'fallback') . '.html';

        $twig = new Twig_Environment(
            new FilesystemLoader(Environment::get('twig_templates_path')),
            ['cache' => Environment::get('twig_cache_path')]
        );

        $output = $twig->render($template, $data);

        $this->config = $config;
        $this->message = (new Message())
            ->withCode($code)
            ->withHeaders(['Content-Type' => Http::CONTENT_HTML])
            ->withBody($output)
        ;
    }

    public function render(): string
    {
        $this->setHeaders();
        return $this->message->body();
    }
}

backend/App.php

namespace backend;

use alkemann\h2l\{ Request, Response };
use backend\response\Twig;

class App
{
    public static function city(Request $r): Response {
        $data = [
            'name' => $r->param('city'),
            'street' => 'Main str.',
            'cars' => ['Volva', 'Tesla', 'Subaru']
        ];
        return new Twig($data, 200, ['template' => 'city']);
    }
}

configs/environment.php

use alkemann\h2l\Environment;

Environment::set([
    'debug' => true,
    'twig_templates_path' => $ROOT . 'templates' . DIRECTORY_SEPARATOR,
    'twig_cache_path' => $ROOT . 'cache' . DIRECTORY_SEPARATOR,
]);

configs/routes.php


use alkemann\h2l\{ Router, Request, Response };
use backend\response\Twig;

Router::add(
    '/', // i.e. http://localhost:8088/
    function(Request $r): Response {
        return new Twig(['name' => 'Johnny Cash'], 200, ['template' => 'home']);
    }
);
// i.e. http://localhost:8088/city/Berlin
Router::add('|city/(?<city>\w+)|', 'backend\App::city');

templates/city.html

<h1>Hello !</h1><html>
    <head>
        <title></title>
    </head>
    <body>
        <h1>City </h1>
        <sub>Europe/Paris</sub>
        <p>On </p>
        <h3>Cars:</h3>
        <ul>
        	
        </ul>
    </body>
<html>

webroot/index.php

$ROOT = realpath(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR;
$VENDOR_PATH = $ROOT . 'vendor' . DIRECTORY_SEPARATOR;
require_once($VENDOR_PATH . 'autoload.php');
require_once($ROOT . 'configs' . DIRECTORY_SEPARATOR . 'environment.php');
$dispatch = new alkemann\h2l\Dispatch($_REQUEST, $_SERVER, $_GET, $_POST);
$dispatch->setRouteFromRouter();
$response = $dispatch->response();
echo ($response) ? $response->render() : '';