0%

slime/twig-view

Templates

Slim does not have a view layer like traditional MVC frameworks. Instead, Slim’s “view” is the HTTP response. Each Slim application route is responsible for preparing and returning an appropriate PSR-7 response object.

Slim’s “view” is the HTTP response.

That being said, the Slim project provides the Twig-View and PHP-View components to help you render templates to a PSR7 Response object.

The slim/twig-view component

The Twig-View PHP component helps you render Twig templates in your application. This component is available on Packagist, and it’s easy to install with Composer like this:

1
composer require slime/twig-view

Next, you need to add the slim/twig-view middleware to the Slim app:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
use Slim\Factory\AppFactory;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;

require __DIR__ . '/vendor/autoload.php';

// Create App
$app = AppFactory::create();

// Create Twig
$twig = Twig::create('path/to/templates', ['cache' => false]); // 设置view的路劲

// Add Twig-View Middleware
$app->add(TwigMiddleware::create($app, $twig));

Note : For production scenarios, cache should be set to some ‘path/to/cache’ to store compiled templates (thus avoiding recomplication on every request). For more information, see Twig environment options

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:

1
2
3
4
5
6
7
8
9
app->get('/hello/{name}', function ($request, $response, $args) {
$view = Twig::fromRequest($request);
return $view->render($response, 'profile.html', [
'name' => $args['name']
]);
})->setName('profile');

// Run app
$app->run();

In this example, $view invoked inside the route callback is a reference to the \Slim\Views\Twig instance returned by the fromRequest method. 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.

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
2
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.

1
2
3
4
5
6
7
8
{% extends "layout.html" %}

{% block body %}
<h1>User List</h1>
<ul>
<li><a href="{{ url_for('profile', { 'name': 'josh' }) }}">Josh</a></li>
</ul>
{% endblock %}

实例:

app/routes.php

1
2
3
4
5
6
7
// 云手机相关routes
$app->group('/cloud/v1', function(Group $group) {
// 支付页面
$group->get('/pay_now_temp',TemplateController::class.':payNowTemp');
// 续费页面
$group->get('/pay_renew_temp', TemplateController::class.':payRenewTemp');
});

TemplateController.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php

declare(strict_types=1);

namespace App\Application\Controllers;


use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Views\Twig;

class TemplateController
{

public function payNowTemp(Request $request, Response $response) : Response
{
$view = Twig::fromRequest($request);
return $view->render($response, 'cloud-vip/index.html', [
'name' => 11
]);
}

public function payRenewTemp(Request $request, Response $response) : Response
{
$view = Twig::fromRequest($request);
return $view->render($response, 'cloud-vip/renew.html', [
'name' => 11
]);
}
}