@@ -118,7 +118,7 @@ use Psr\Http\Message\ServerRequestInterface as Request;
118
118
use Psr\Http\Message\ResponseInterface as Response;
119
119
120
120
$app->get('/hello/{name}', function (Request $request, Response $response) {
121
- $response->getBody()->write(" Hello!" );
121
+ $response->getBody()->write(' Hello!' );
122
122
return $response;
123
123
});
124
124
@@ -155,3 +155,49 @@ $app = new class() extends \DI\Bridge\Slim\App {
155
155
```
156
156
157
157
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