-
Notifications
You must be signed in to change notification settings - Fork 60
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
Comments
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. |
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. |
I think this should be for v3 of PHP-View. |
The latest version of Twig-View has this render method signature: public function render(ResponseInterface $response, string $template, array $data = []): ResponseInterface The PHP-View 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 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. |
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". |
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
The text was updated successfully, but these errors were encountered: