1. Go to this page and download the library: Download slim/twig-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 / twig-view example snippets
use DI\Container;
use Slim\Factory\AppFactory;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;
class, function() {
return Twig::create(__DIR__ . '/../templates', ['cache' => 'path/to/cache']);
});
// Create App from container
$app = AppFactory::createFromContainer($container);
// Add Twig-View Middleware
$app->add(TwigMiddleware::create($app, $container->get(Twig::class)));
// Add other middleware
$app->addRoutingMiddleware();
$app->addErrorMiddleware(true, true, true);
// Render from template file templates/profile.html.twig
$app->get('/hello/{name}', function ($request, $response, $args) {
$viewData = [
'name' => $args['name'],
];
$twig = $this->get(Twig::class);
return $twig->render($response, 'profile.html.twig', $viewData);
})->setName('profile');
// Render from string
$app->get('/hi/{name}', function ($request, $response, $args) {
$viewData = [
'name' => $args['name'],
];
$twig = $this->get(Twig::class);
$str = $twig->fetchFromString('<p>Hi, my name is {{ name }}.</p>', $viewData);
$response->getBody()->write($str);
return $response;
});
// Run app
$app->run();
use Slim\Factory\AppFactory;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;
'path/to/templates', ['cache' => 'path/to/cache']);
// Add Twig-View Middleware
$app->add(TwigMiddleware::create($app, $twig));
// Define named route
$app->get('/hello/{name}', function ($request, $response, $args) {
$view = Twig::fromRequest($request);
return $view->render($response, 'profile.html.twig', [
'name' => $args['name']
]);
})->setName('profile');
// Render from string
$app->get('/hi/{name}', function ($request, $response, $args) {
$view = Twig::fromRequest($request);
$str = $view->fetchFromString(
'<p>Hi, my name is {{ name }}.</p>',
[
'name' => $args['name']
]
);
$response->getBody()->write($str);
return $response;
});
// Run app
$app->run();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.