germania-kg/renderer

Render Callables for PHP files, Twig, Smarty and Markdown

3.0.4 2022-05-30 13:50 UTC

README

Render Callables for PHP files, Twig, Smarty and Markdown

Packagist PHP version Build Status Scrutinizer Code Quality Code Coverage Build Status

Installation with Composer

$ composer require germania-kg/renderer
"require": {
    "germania-kg/renderer":"^1.0|^2.0|^3.0"
}

The v3 release of germania-kg/renderer supports Twig v2 and v3.

Usage

All classes PhpRenderer, TwigRenderer, RenderedMarkdownRenderer, and SmartyRenderer extend \Germania\Renderer\RendererAbstract and implement the RendererInterface:

interface RendererInterface {
    /**
     * @param  string   $template   The template file
     * @param  array    $context    Associative template variables array
     * @return string   Template output
     */	
     public function render( $template, array $context = array()) : string
  
    /**
     * Callable alias for render()
     */	
     public function __invoke( $template, array $context = array())
}

PhpRenderer

This RendererInterface implentation will include a PHP file, using output buffering. Passed context variables are extracted to __invoke method scope und thus are locally available inside the PHP include file.

<?php
use Germania\Renderer\PhpRenderer;

// Base path and PSR-3 Logger are optional.
// Base path defaults to PHP's getcwd()
$php = new PhpRenderer;
$php = new PhpRenderer( '/path/to/includes' );
$php = new PhpRenderer( array('/path/to/includes', '/another/path') );
$php = new PhpRenderer( '/path/to/includes', $logger );

// Pass file name and variable context:
echo $php('myinc.php', [
	'foo'  => 'bar',
	'user' => $container->get('var')
]);

PSR-7 HTTP Messages

If the include file itselfs returns a PSR-7 ResponseInterface, the PhpRenderer will return this ResponseInterface instance.

<?php
// myinc.php
return $response;
<?php
use Psr\Http\Message\ResponseInterface;

$render = new PhpRenderer;

$result = $render('myinc.php', [
	'response' => new GuzzleHttp\Psr7\Response
]);

echo $result instanceOf ResponseInterface
? $result->getBody()
: $result;

TwigRenderer

<?php
use Germania\Renderer\TwigRenderer;

// Have your Twig_Environment at hand;
// Logger is optional.
$render_twig = new TwigRenderer( $twig, $logger ) ;

// Pass file name and variable context:
echo $render_twig('mytwig.tpl', [
	'foo'  => 'bar',
	'user' => $container->get('var')
]);

SmartyRenderer

<?php
use Germania\Renderer\SmartyRenderer;

// Have your Smarty3 at hand;
// Logger is optional.
$render_smarty = new SmartyRenderer( $smarty ) ;
$render_smarty = new SmartyRenderer( $smarty, $logger ) ;

// Pass file name and variable context:
echo $render_smarty('mysmarty.tpl', [
	'foo'  => 'bar',
	'user' => $container->get('var')
]);

RenderedMarkdownRenderer

Sometimes it is useful to 'process' a markdown source code first with a real template engine before markdown-parsing. RenderedMarkdownRenderer takes a RendererInterface instance and any of Carsten Brandt's cebe/markdown flavours.

<?php
use Germania\Renderer\TwigRenderer;
use Germania\Renderer\RenderedMarkdownRenderer;
use cebe\markdown\Markdown;

// Have a RendererInterface instance at hand,
// as well as Carsten Brandt's Markdown Parser.

$twig_render = new TwigRenderer( $twig, $logger );
$markdown = new Markdown;

// Pass them to constructor:
$rendered_markdown_renderer = new RenderedMarkdownRenderer($twig_render, $markdown);

// Pass file name and variable context:
echo $rendered_markdown_renderer('twigged_markdown.md', [
	'foo'  => 'bar',
	'user' => 'Joe'
]);

Issues

…As always, some documentation missing here and there. Stay up to date on issues list.

Development

$ git clone https://github.com/GermaniaKG/Renderer.git
$ cd Renderer
$ composer install

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