# 模板
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->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 中。