• league/plates slim/twig-view ## slim/twig-view 组件 The Slim Framework does provide an optional, standalone component named slim/twig-view. This standalone component is a Pimple service that provides a Twig templating system for your Slim application. ### Install with Composer First, install the slim/twig-view component with Composer. Execute this bash command in your project's root directory. composer require slim/twig-view ### Register the view service The slim/twig-view component must be registerd with the Slim application before you invoke your application's run() method. php // Create Slim app $app = new \Slim\App(); // Register Twig View service $app->register(new \Slim\Views\Twig('path/to/templates', [ 'cache' => 'path/to/cache' ])); // Define your routes here... // Run app $app->run(); The \Slim\Views\Twig constructor's first argument is the relative or absolute path to the filesystem directory that contains your Twig templates. The constructor's second argument is an associative array of Twig environment settings. ### Use the view service After you register the slim/twig-view component, you can access the view anywhere in your Slim application with $app['view']. This example demonstrates how to render a template with the Twig View service. php // Create Slim app $app = new \Slim\App(); // Register Twig View helper $app->register(new \Slim\Views\Twig('path/to/templates', [ 'cache' => 'path/to/cache' ])); // Define named route $app->get('/hello/{name}', function ($request, $response, $args) { $this['view']->render('profile.html', [ 'name' => $args['name'] ]); })->setName('profile'); // Run app $app->run(); ### The url_for() method The slim/twig-view component exposes a custom url_for() function to your Twig templates. You can use this function to generate complete URLs to any named route in your Slim application. The url_for() function accepts two arguments: 1. A route name 2. A hash of route placeholder names and replacement values The second argument's keys should correspond to the selected route's pattern placeholders. This is an example Twig template that draws a link URL for the "profile" named route shown in the example Slim application above. ``` {% extends "layout.html" %} {% block body %}">概览 A Slim Framework application's view is the HTTP response. If you want to send content to the HTTP client, you must write the appropriate content to the HTTP response body. There are two ways you can write content to the HTTP response body. ## HTTP 响应 ### Write to the Response object You can write directly to the Slim application's Response object using the Response object's write() method. This is an example scenario inside a Slim application route: php $app->get('/user/{id:\d+}', function ($request, $response, $args) { $response->write(sprintf( 'You are viewing user with ID: %s', $args['id'] )); return $response; }); We write a message directly to the Response object, and we return the response object from the route callback routine when ready. ### Write to the output buffer We can also write to the current output buffer. Any content captured by the current output buffer will be appended to the Response object's body after the route callback routine completes. This example demonstrates this scenario inside a Slim application route: php $app->get('/user/{id:\d+}', function ($request, $response, $args) { echo sprintf( 'You are viewing user with ID: %s', $args['id'] ); }); # 模板 The Slim Framework does not provide a built-in templating system. Instead, you may generate output using third-party template-related tools most appropriate for your Slim application. Whichever tools you choose, they are still bound by the rules above: content must be written directly to the Response object or captured into the current output buffer. Some great third-party tools for generating templated output include: league/plates slim/twig-view ## slim/twig-view 组件 The Slim Framework does provide an optional, standalone component named slim/twig-view. This standalone component is a Pimple service that provides a Twig templating system for your Slim application. ### Install with Composer First, install the slim/twig-view component with Composer. Execute this bash command in your project's root directory. composer require slim/twig-view ### Register the view service The slim/twig-view component must be registerd with the Slim application before you invoke your application's run() method. php // Create Slim app $app = new \Slim\App(); // Register Twig View service $app->register(new \Slim\Views\Twig('path/to/templates', [ 'cache' => 'path/to/cache' ])); // Define your routes here... // Run app $app->run(); The \Slim\Views\Twig constructor's first argument is the relative or absolute path to the filesystem directory that contains your Twig templates. The constructor's second argument is an associative array of Twig environment settings. ### Use the view service After you register the slim/twig-view component, you can access the view anywhere in your Slim application with $app['view']. This example demonstrates how to render a template with the Twig View service. php // Create Slim app $app = new \Slim\App(); // Register Twig View helper $app->register(new \Slim\Views\Twig('path/to/templates', [ 'cache' => 'path/to/cache' ])); // Define named route $app->get('/hello/{name}', function ($request, $response, $args) { $this['view']->render('profile.html', [ 'name' => $args['name'] ]); })->setName('profile'); // Run app $app->run(); ### The url_for() method The slim/twig-view component exposes a custom url_for() function to your Twig templates. You can use this function to generate complete URLs to any named route in your Slim application. The url_for() function accepts two arguments: 1. A route name 2. A hash of route placeholder names and replacement values The second argument's keys should correspond to the selected route's pattern placeholders. This is an example Twig template that draws a link URL for the "profile" named route shown in the example Slim application above. ``` {% extends "layout.html" %} {% block body %}
  • User List

    概览 A Slim Framework application's view is the HTTP response. If you want to send content to the HTTP client, you must write the appropriate content to the HTTP response body. There are two ways you can write content to the HTTP response body. ## HTTP 响应 ### Write to the Response object You can write directly to the Slim application's Response object using the Response object's write() method. This is an example scenario inside a Slim application route: php $app->get('/user/{id:\d+}', function ($request, $response, $args) { $response->write(sprintf( 'You are viewing user with ID: %s', $args['id'] )); return $response; }); We write a message directly to the Response object, and we return the response object from the route callback routine when ready. ### Write to the output buffer We can also write to the current output buffer. Any content captured by the current output buffer will be appended to the Response object's body after the route callback routine completes. This example demonstrates this scenario inside a Slim application route: php $app->get('/user/{id:\d+}', function ($request, $response, $args) { echo sprintf( 'You are viewing user with ID: %s', $args['id'] ); }); # 模板 The Slim Framework does not provide a built-in templating system. Instead, you may generate output using third-party template-related tools most appropriate for your Slim application. Whichever tools you choose, they are still bound by the rules above: content must be written directly to the Response object or captured into the current output buffer. Some great third-party tools for generating templated output include: league/plates slim/twig-view ## slim/twig-view 组件 The Slim Framework does provide an optional, standalone component named slim/twig-view. This standalone component is a Pimple service that provides a Twig templating system for your Slim application. ### Install with Composer First, install the slim/twig-view component with Composer. Execute this bash command in your project's root directory. composer require slim/twig-view ### Register the view service The slim/twig-view component must be registerd with the Slim application before you invoke your application's run() method. php // Create Slim app $app = new \Slim\App(); // Register Twig View service $app->register(new \Slim\Views\Twig('path/to/templates', [ 'cache' => 'path/to/cache' ])); // Define your routes here... // Run app $app->run(); The \Slim\Views\Twig constructor's first argument is the relative or absolute path to the filesystem directory that contains your Twig templates. The constructor's second argument is an associative array of Twig environment settings. ### Use the view service After you register the slim/twig-view component, you can access the view anywhere in your Slim application with $app['view']. This example demonstrates how to render a template with the Twig View service. php // Create Slim app $app = new \Slim\App(); // Register Twig View helper $app->register(new \Slim\Views\Twig('path/to/templates', [ 'cache' => 'path/to/cache' ])); // Define named route $app->get('/hello/{name}', function ($request, $response, $args) { $this['view']->render('profile.html', [ 'name' => $args['name'] ]); })->setName('profile'); // Run app $app->run(); ### The url_for() method The slim/twig-view component exposes a custom url_for() function to your Twig templates. You can use this function to generate complete URLs to any named route in your Slim application. The url_for() function accepts two arguments: 1. A route name 2. A hash of route placeholder names and replacement values The second argument's keys should correspond to the selected route's pattern placeholders. This is an example Twig template that draws a link URL for the "profile" named route shown in the example Slim application above. ``` {% extends "layout.html" %} {% block body %}

    User List

    • Josh }})

      {% endblock %} ```