slimphp / PHP-View

A Simple PHP Renderer for Slim 3 & 4 (or any other PSR-7 project)
MIT License
264 stars 60 forks source link

Use same API as Twig-View for data/attributes #24

Closed aurmil closed 2 months ago

aurmil commented 8 years ago

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

geggleto commented 8 years ago

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.

terah commented 8 years ago

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.

akrabat commented 7 years ago

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

odan commented 4 years ago

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 commented 2 months ago

Thanks for the contribution. The main purpose and scope of this package is PHP template rendering. We do not plan to support rendering of different file types like PDF, CSV, etc. So the current solution should be "good enough".