Dark Mode

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

GermaniaKG/TwigRequestHandler

Repository files navigation


Germania KG * RequestHandler for Twig

Installation

$ composer require germania-kg/twig-requesthandler

Setup

The constructor accepts a Twig Environment and PSR-17 ResponseFactory. This example uses Tobias Nyholm's nyholm/psr7 package: composer require nyholm/psr7



use Germania\TwigRequestHandler\TwigRequestHandler;
use Twig\Environment as Twig;
use Nyholm\Psr7\Factory\Psr17Factory;

// Dependencies
$twig = new Twig( ... );
$psr17Factory = new Psr17Factory;

// Instantiation
$request_handler = new TwigRequestHandler($twig, $psr17Factory);

Usage

Have a ServerRequest at hand and configure it with a template attribute and a context attribute.

  • The template attribute must be a string as required by Twig.
  • The context attribute must be an array as required by Twig; Instances of ArrayObject will be converted.

N.B. Invalid variable types will lead to a RuntimeException at runtime on request handling, not during configuration!

withAttribute('template', 'website.twig') ->withAttribute('context', [ 'title' => 'The Website title', 'company' => 'ACME corp.' ]);">
$request = $psr17Factory->createServerRequest('GET', 'http://tnyholm.se');

$request = $request
->withAttribute('template', 'website.twig')
->withAttribute('context', [
'title' => 'The Website title',
'company' => 'ACME corp.'
]);

Now, the above RequestHandler can be used as normal:

$response = $request_handler->handle($request);

echo $response->getBody()->__toString();
// s.th. like
// "The Website title * ACME corp."

Configuration

You can change these default settings:

$request_handler->setTwig($twig);
$request_handler->setResponseFactory($another);

You can change these default settings

$request_handler->setTemplateAttributeName("template")
->setContextAttributeName("context")
->setResponseContentType("text/html")
->setResponseStatusCode(200);

... and the core components:

$request_handler->setTwig($twig)
->setResponseFactory($another);

Issues

See full issues list.

Development

Grab and go using one of these:

$ git clone git@github.com:GermaniaKG/TwigRequestHandler.git
$ gh repo clone GermaniaKG/TwigRequestHandler

Unit tests

Either copy phpunit.xml.dist to phpunit.xml and adapt to your needs, or leave as is. Run PhpUnit test or composer scripts like this:

$ composer test
# or
$ vendor/bin/phpunit

About

PSR-15 RequestHandler for rendering Twig templates

Resources

Readme

Stars

Watchers

Forks

Packages

Contributors

Languages