Skip to content

Commit 44855b9

Browse files
committed
Add Twig documentation section
1 parent e9f0bf4 commit 44855b9

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

README.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ use Psr\Http\Message\ServerRequestInterface as Request;
118118
use Psr\Http\Message\ResponseInterface as Response;
119119

120120
$app->get('/hello/{name}', function (Request $request, Response $response) {
121-
$response->getBody()->write("Hello!");
121+
$response->getBody()->write('Hello!');
122122
return $response;
123123
});
124124

@@ -155,3 +155,49 @@ $app = new class() extends \DI\Bridge\Slim\App {
155155
```
156156

157157
Have a look at [configuring PHP-DI](http://php-di.org/doc/container-configuration.html) for more details.
158+
159+
### Twig
160+
161+
In order to get you started easily, here is how you can install the Twig extension for Slim:
162+
163+
- install the [Twig-View](https://github.com/slimphp/Twig-View) package:
164+
165+
```
166+
composer require slim/twig-view
167+
```
168+
- configure the `Twig` class in PHP-DI (taken from [the package's documentation](https://github.com/slimphp/Twig-View#usage)):
169+
170+
```php
171+
class MyApp extends \DI\Bridge\Slim\App
172+
{
173+
protected function configureContainer(ContainerBuilder $builder)
174+
{
175+
$definitions = [
176+
177+
\Slim\Views\Twig::class => function (ContainerInterface $c) {
178+
$twig = new \Slim\Views\Twig('path/to/templates', [
179+
'cache' => 'path/to/cache'
180+
]);
181+
182+
$twig->addExtension(new \Slim\Views\TwigExtension(
183+
$c->get('router'),
184+
$c->get('request')->getUri()
185+
));
186+
187+
return $twig;
188+
},
189+
190+
];
191+
192+
$builder->addDefinitions($definitions);
193+
}
194+
}
195+
```
196+
197+
You can now inject the service in your controllers and render templates:
198+
199+
```php
200+
$app->get('/', function ($response, Twig $twig) {
201+
return $twig->render($response, 'home.twig');
202+
});
203+
```

0 commit comments

Comments
 (0)