2017 © Pedro Peláez
 

library renderer

Render Callables for PHP files, Twig, Smarty and Markdown

image

germania-kg/renderer

Render Callables for PHP files, Twig, Smarty and Markdown

  • Monday, February 5, 2018
  • by germania-kg
  • Repository
  • 2 Watchers
  • 0 Stars
  • 79 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 15 Versions
  • 0 % Grown

The README.md

Germania KG · Renderer

Render Callables for PHP files, Twig, Smarty and Markdown, (*1)

Packagist PHP version Build Status Scrutinizer Code Quality Code Coverage Build Status, (*2)

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., (*3)

Usage

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

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., (*5)

<?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., (*6)

<?php
// myinc.php
return $response;

```php <?php use Psr\Http\Message\ResponseInterface;, (*7)

$render = new PhpRenderer;, (*8)

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

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



## TwigRenderer ```php <?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., (*10)

<?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., (*11)

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:, (*12)

$ composer test
# or
$ vendor/bin/phpunit

The Versions

05/02 2018

dev-develop

dev-develop

Render Callables for PHP files, Twig, Smarty and Markdown

  Sources   Download

MIT

The Requires

 

The Development Requires

by Carsten Witt

05/02 2018

dev-master

9999999-dev

Render Callables for PHP files, Twig, Smarty and Markdown

  Sources   Download

MIT

The Requires

 

The Development Requires

by Carsten Witt

05/02 2018

1.1.4

1.1.4.0

Render Callables for PHP files, Twig, Smarty and Markdown

  Sources   Download

MIT

The Requires

 

The Development Requires

by Carsten Witt

02/02 2018

1.1.3

1.1.3.0

Render Callables for PHP files, Twig, Smarty and Markdown

  Sources   Download

MIT

The Requires

 

The Development Requires

by Carsten Witt

31/08 2017

1.1.2

1.1.2.0

Render Callables for PHP files, Twig, Smarty and Markdown

  Sources   Download

MIT

The Requires

 

The Development Requires

by Carsten Witt

27/04 2017

1.0.5

1.0.5.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by Carsten Witt

04/01 2017

1.0.4

1.0.4.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by Carsten Witt

20/12 2016

1.0.3

1.0.3.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by Carsten Witt

08/12 2016

1.0.2

1.0.2.0

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Carsten Witt

04/11 2016

1.0.1

1.0.1.0

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Carsten Witt

26/10 2016

1.0.0

1.0.0.0

  Sources   Download

proprietary

The Requires

 

The Development Requires

by Carsten Witt