Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
aurmil opened this issue Mar 15, 2016 · 5 comments
Closed

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

aurmil opened this issue Mar 15, 2016 · 5 comments
Assignees
Milestone

Comments

@aurmil
Copy link

aurmil commented Mar 15, 2016

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
Copy link
Member

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
Copy link

terah commented Jul 16, 2016

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 akrabat removed this from the 2.2 milestone Oct 11, 2016
@akrabat
Copy link
Member

akrabat commented Oct 11, 2016

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

@geggleto geggleto added this to the 3.x milestone May 26, 2017
@odan
Copy link
Contributor

odan commented Dec 19, 2019

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
Copy link
Contributor

odan commented Jul 9, 2024

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".

@odan odan closed this as completed Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants