🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 模板 Slim 没有传统 MVC 框架的视图(view)层。相反,Slim 的“视图”就是 HTTP 响应。Slim 应用程序的每个路由都为准备和返回恰当的 PSER 7 响应对象负责。 > Slim’s “view” is the HTTP response. 话虽如此,但 Slim 项目提供了 [Twig-View](#the-slimtwig-view-component) 和 [PHP-View](#the-slimphp-view-component) 组件帮助你将模版渲染为 PSR7 响应对象。 ## The slim/twig-view 组件 [Twig-View](https://github.com/slimphp/Twig-View) PHP 组件帮助你渲染应用程序中的 [Twig](http://twig.sensiolabs.org/) 模版。这个组件可以在 Packageist 上找到。可以像这样使用 composer 轻易地安装: ``` composer require slim/twig-view ``` Figure 1: Install slim/twig-view component. 下一步,你需要在 Slim 应用容器中将此组将注册为服务,像这样: ``` <?php // Create app $app = new \Slim\App(); // Get container $container = $app->getContainer(); // Register component on container $container['view'] = function ($container) { $view = new \Slim\Views\Twig('path/to/templates', [ 'cache' => 'path/to/cache' ]); $view->addExtension(new \Slim\Views\TwigExtension( $container['router'], $container['request']->getUri() )); return $view; }; ``` Figure 2: Register slim/twig-view component with container. 记住:“cache” 可以设置为 false 禁用它,‘auto_reload’ 选项也是如此,这在开发环境中很有用。了解更多信息,查阅: [Twig environment options](http://twig.sensiolabs.org/api/master/Twig_Environment.html#method___construct) 现在你可以使用应用程序内部的 `slim/twig-view` 组件服务并将其写入到 PSR 7 响应对象中,像这样: ``` // Render Twig template in route $app->get('/hello/{name}', function ($request, $response, $args) { return $this->view->render($response, 'profile.html', [ 'name' => $args['name'] ]); })->setName('profile'); // Run app $app->run(); ``` Figure 3: Render template with slim/twig-view container service. 在这个例子中,在路由回调中被调用的 `$this-&gt;view`,是容器服务返回的 `\Slim\Views\Twig` 实例的一个参考(reference)。`\Slim\Views\Twig` 实例的 `render()` 方法接收一个 PSR7 响应对象作为它的第一个参数,Twig 模版路径作为它的第二个参数,模板变量的数组作为它的最后一个参数。这个 `render()` 方法返回一个新的 PSR7 响应对象,它的响应体是由 Twig 模版渲染的。 ### path_for() 方法 `slim/twig-view` 组件为 Twig 模版暴露了一个自定义 `path_for()` 函数。你可以使用这个函数生成完整的指向应用程序中任意已命名路由的 URL。`path_for()` 函数接收两个参数: 1. 路由名称 2. 路由占位符和替换值的散列(hash) 第二个参数的关键字须与已选择的路由的模式占位符一致。这是一个示例的 Twig 模版,它描述了上面的示例 Slim 应用程序中的 “profile” 路由的链接 URL。 ``` {% extends "layout.html" %} {% block body %} <h1>User List</h1> <ul> <li><a href="{{ path_for('profile', { 'name': 'josh' }) }}">Josh</a></li> </ul> {% endblock %} ``` ## slim/php-view 组件 [PHP-View](https://github.com/slimphp/PHP-View) PHP 组件帮助你渲染 PHP 模版。该组件可以在 Packagist 上找到,像这样使用 Composer 安装: ``` composer require slim/php-view ``` Figure 4: Install slim/php-view component. 在 Slim App 的容器中,将此组件注册为服务,这么做: ``` <?php // Create app $app = new \Slim\App(); // Get container $container = $app->getContainer(); // Register component on container $container['view'] = function ($container) { return new \Slim\Views\PhpRenderer('path/to/templates/with/trailing/slash/'); }; ``` Figure 5: Register slim/php-view component with container. 使用 view 组件渲染 PHP 视图: ``` // Render Twig template in route $app->get('/hello/{name}', function ($request, $response, $args) { return $this->view->render($response, 'profile.html', [ 'name' => $args['name'] ]); })->setName('profile'); // Run app $app->run(); ``` Figure 6: Render template with slim/twig-view container service. ## 其他模版系统 并不限于使用 `Twig-View` 和 `PHP-View` 组件。你可以使用任意 PHP 模版系统,只要它能渲染你的模版,并最终输出到 PSR7 响应对象的 body 中。