PHP code example of germania-kg / twig-requesthandler

1. Go to this page and download the library: Download germania-kg/twig-requesthandler library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

germania-kg / twig-requesthandler example snippets




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


$request = $psr17Factory->createServerRequest('GET', 'http://tnyholm.se');

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

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

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

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

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

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