1. Go to this page and download the library: Download slim/php-view library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
slim / php-view example snippets
//Construct the View
$renderer = new PhpRenderer('path/to/templates');
$viewData = [
'key1' => 'value1',
'key2' => 'value2',
];
// Render a template
$response = $renderer->render(new Response(), 'hello.php', $viewData);
use Slim\AppFactory;
use Slim\Views\PhpRenderer;
, function ($request, $response) {
$renderer = new PhpRenderer('path/to/templates');
$viewData = [
'name' => 'John',
];
return $renderer->render($response, 'hello.php', $viewData);
});
$app->run();
use Psr\Container\ContainerInterface;
use Slim\Views\PhpRenderer;
// ...
return [
PhpRenderer::class => function (ContainerInterface $container) {
$renderer = new PhpRenderer('path/to/templates');
return $renderer;
},
];
// Via the constructor
$globalViewData = [
'title' => 'Title'
];
$renderer = new PhpRenderer('path/to/templates', $globalViewData);
// or setter
$viewData = [
'key1' => 'value1',
'key2' => 'value2',
];
$renderer->setAttributes($viewData);
// or individually
$renderer->addAttribute($key, $value);
$viewData = [
'title' => 'Title'
];
$renderer = new PhpRenderer('path/to/templates', $viewData);
//...
$response = $renderer->render($response, $template, [
'title' => 'My Title'
]);
// In the view above, the $title will be "My Title" and not "Title"