twigHow to use Slim with PHP Twig?
Slim is a PHP micro-framework that can be used with Twig, a template engine for PHP. To use Slim with Twig, you need to install the Slim-Views package.
composer require slim/slim-views
Once installed, you can configure Slim to use Twig as the default template engine.
$container['view'] = function ($container) {
$view = new \Slim\Views\Twig(__DIR__ . '/../templates', [
'cache' => false
]);
// Instantiate and add Slim specific extension
$basePath = rtrim(str_ireplace('index.php', '', $container['request']->getUri()->getBasePath()), '/');
$view->addExtension(new Slim\Views\TwigExtension($container['router'], $basePath));
return $view;
};
The code above configures Slim to use Twig as the default template engine. It also adds the Slim-specific Twig extension which allows you to use the urlFor
function to generate URLs.
Parts of the code:
$container['view']
: This is the Slim container which holds the view configuration.new \Slim\Views\Twig
: This instantiates a new Twig view.$basePath
: This is the base path of the request.$view->addExtension
: This adds the Slim-specific Twig extension.
Helpful links
More of Twig
- How to integrate Twig with Yii2?
- How to use var_dump with PHP and Twig?
- How to use Twig in PHP to get the current year?
- How to embed YouTube videos in Twig with PHP?
- How to use XOR in Twig with PHP?
- How to write PHP code in Twig?
- How to use PHP variables in Twig?
- How to set a session variable in PHP Twig?
- How to get the user agent in PHP Twig?
- How to use Twig with a PHP MVC framework?
See more codes...