From 75a82ca1f6306196e97ef9007a4dbca0e062c86b Mon Sep 17 00:00:00 2001 From: Daniel Opitz Date: Tue, 16 Jul 2024 18:22:17 +0200 Subject: [PATCH] Update Twig-View documentation (#727) --- docs/v4/features/twig-view.md | 39 ++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/docs/v4/features/twig-view.md b/docs/v4/features/twig-view.md index 4f46efa7..229065df 100644 --- a/docs/v4/features/twig-view.md +++ b/docs/v4/features/twig-view.md @@ -33,7 +33,7 @@ require __DIR__ . '/../vendor/autoload.php'; $app = AppFactory::create(); // Create Twig -$twig = Twig::create('path/to/templates', ['cache' => false]); +$twig = Twig::create(__DIR__ . '/../templates', ['cache' => false]); // Add Twig-View Middleware $app->add(TwigMiddleware::create($app, $twig)); @@ -47,13 +47,13 @@ Now you can use the `slim/twig-view` component service inside an app route to render a template and write it to a PSR-7 Response object like this: ```php -$app->get('/hello', function ($request, $response) { +$app->get('/', function ($request, $response) { $view = Twig::fromRequest($request); - return $view->render($response, 'profile.html', [ + return $view->render($response, 'home.html.twig', [ 'name' => 'John', ]); -})->setName('profile'); +}); // Run app $app->run(); @@ -63,6 +63,24 @@ In this example, `$view` invoked inside the route callback is a reference to the The `\Slim\Views\Twig` instance's `render()` method accepts a PSR-7 Response object as its first argument, the Twig template path as its second argument, and an array of template variables as its final argument. The `render()` method returns a new PSR-7 Response object whose body is the rendered Twig template. +Create a directory in your project root: `templates/` + +Create a Twig template file within the templates directory: `templates/home.html.twig` + +```html +{% raw %} + + + + Welcome to Slim! + + +

Hello {{ name }}

+ + +{% endraw %} +``` + ### The url_for() method The `slim/twig-view` component exposes a custom `url_for()` function to your Twig templates. @@ -77,13 +95,10 @@ This is an example Twig template that draws a link URL for the "profile" named r ```html {% raw %} -{% extends "layout.html" %} - -{% block body %} -

User List

- -{% endblock %} +Josh {% endraw %} ``` + +## Read more + +* [PHP-View documentation](https://github.com/slimphp/PHP-View) \ No newline at end of file