diff --git a/docs/integrations/templates.md b/docs/integrations/templates.md index 0e241f1..e726c9e 100644 --- a/docs/integrations/templates.md +++ b/docs/integrations/templates.md @@ -1,14 +1,109 @@ # Templates -> ⚠️ **Documentation still under construction** -> -> You're seeing an early draft of the documentation that is still in the works. -> Give feedback to help us prioritize. -> We also welcome [contributors](../getting-started/community.md) to help out! - -* Very common requirement, especially for [HTML pages](../api/response.md#html) -* Any template language possible - * [Twig](https://twig.symfony.com/) - * [Handlebars](https://github.com/salesforce/handlebars-php) - * [Mustache](https://github.com/bobthecow/mustache.php) -* Template files often loaded from [filesystem](filesystem.md) (avoid blocking) +Templates let you render HTML or other text content from separate files, keeping presentation logic out of your request handlers. + +## Twig + +[Twig](https://twig.symfony.com/) is a template language with strong inheritance and extension support. Framework X integrates with it directly. + +Install Twig: + +```bash +composer require twig/twig +``` + +Instantiate the Twig `Environment` and pass it to your controller via dependency injection: + +```php title="public/index.php" + $twig, +])); + +$app->get('/', Acme\Todo\HomeController::class); +$app->run(); +``` + +Inside a controller, render a template and return it as HTML: + +```php title="src/HomeController.php" +twig->render('home.html.twig', [ + 'title' => 'Welcome to Framework X', + ]); + + return Response::html($html); + } +} +``` + +And the template file: + +```html title="templates/home.html.twig" + + +
+