PHP-View icon indicating copy to clipboard operation
PHP-View copied to clipboard

Use same API as Twig-View for data/attributes

Open aurmil opened this issue 9 years ago • 4 comments

hello

the way to pass datas to the view renderer differs when using PHP-View and Twig-View I think this is not the right way maybe PhpRenderer should also implement \ArrayAccess like Twig class does

regards

aurmil avatar Mar 15 '16 09:03 aurmil

Yeah I could see this being useful. I will speak with @akrabat about this. We probably should have an interface defining the render methods that way the community can just create their own for whatever template engine they want.

geggleto avatar Mar 15 '16 12:03 geggleto

Came here to suggest this.

I'd like this in an interface as I'd like to build a bunch of view renderers for various output formats;


interface ViewRenderer
{
    public function render(ResponseInterface $response, array $data = [], string $template='') : ResponseInterface;
}

Then we could have:

use Slim\Views\PhpRenderer;
use Terah\Views\PdfRenderer;
use Terah\Views\CsvRenderer;

include "vendor/autoload.php";

$app = new Slim\App();
$container = $app->getContainer();
$container['html_renderer'] = new PhpRenderer("./templates");
$container['pdf_renderer'] = new PdfRenderer(new WkhtmlToPdfWrapper());
$container['csv_renderer'] = new CsvRenderer(new League\Csv());


$app->get('/hello/{name}.pdf', function ($request, $response, $args) {
    return $this->pdf_renderer->render(
        $this->html_renderer->render($response, $args, "/hello.php")
    );
});

$app->get('/hello/{name}.csv', function ($request, $response, $args) {

    $args['data'] = getSomeData();
    return $this->csv_render->render($response, $args);
});

$app->run();

I've made a start to this (less the Renderer interface) here: https://github.com/terah/view

It's very much a work in progress but before I plough ahead I'd like to get some community input.

terah avatar Jul 16 '16 04:07 terah

I think this should be for v3 of PHP-View.

akrabat avatar Oct 11 '16 07:10 akrabat

The latest version of Twig-View has this render method signature:

public function render(ResponseInterface $response, string $template, array $data = []): ResponseInterface

The PHP-View PhpRenderer.php class has this signature:

public function render(ResponseInterface $response, string $template, array $data = []): ResponseInterface

So they are exactly the same.

But there a some tiny difference:

Twig-View throws 3 different exceptions:

* @throws LoaderError  When the template cannot be found
* @throws SyntaxError  When an error occurred during compilation
* @throws RuntimeError When an error occurred during rendering

PHP-View could could throw @throws Throwable

This means we could add a common interface like this:

<?php

namespace Slim\Contract;

use Psr\Http\Message\ResponseInterface;
use Throwable;

interface ResponseRendererInterface
{
    /**
     * Creates a Response object from view template.
     *
     * @param ResponseInterface $response The response
     * @param string $template The template pathname relative to templates directory
     * @param array $data The associative array of template variables
     *
     * @throws Throwable
     *
     * @return ResponseInterface
     */
    public function render(ResponseInterface $response, string $template, array $data = []): ResponseInterface;
}

This interface should be placed in a new "Slim-Contract" repository so that we could implement it in both libraries.

odan avatar Dec 19 '19 21:12 odan