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;
ew in Container
$container->set('view', function() {
return Twig::create('path/to/templates', ['cache' => 'path/to/cache']);
});
// Create App
$app = AppFactory::create();
// Add Twig-View Middleware
$app->add(TwigMiddleware::createFromContainer($app));
// Define named route
$app->get('/hello/{name}', function ($request, $response, $args) {
return $this->get('view')->render($response, 'profile.html', [
'name' => $args['name']
]);
})->setName('profile');
// Render from string
$app->get('/hi/{name}', function ($request, $response, $args) {
$str = $this->get('view')->fetchFromString(
'<p>Hi, my name is {{ name }}.</p>',
[
'name' => $args['name']
]
);
$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', [
'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();