quagga/twig-view

Slim Framework 4视图助手,基于Twig 3模板组件构建

3.3.0 2024-04-13 05:47 UTC

This package is auto-updated.

Last update: 2024-09-13 07:46:35 UTC


README

Build Status Coverage Status License

这是一个基于Twig模板组件构建的Slim Framework视图助手。您可以使用此组件在Slim Framework应用程序中创建和渲染模板。

安装

通过Composer

$ composer require slim/twig-view

需要Slim Framework 4、Twig 3和PHP 7.4或更高版本。

用法

use DI\Container;
use Slim\Factory\AppFactory;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;

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

// Create Container
$container = new Container();
AppFactory::setContainer($container);

// Set view in Container
$container->set('view', function() {
    return Twig::create('path/to/templates', ['cache' => 'path/to/cache']);
});

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

// Add Twig-View Middleware
$app->add(TwigMiddleware::createFromContainer($app));

// Define named route
$app->get('/hello/{name}', function ($request, $response, $args) {
    return $this->get('view')->render($response, 'profile.html', [
        'name' => $args['name']
    ]);
})->setName('profile');

// Render from string
$app->get('/hi/{name}', function ($request, $response, $args) {
    $str = $this->get('view')->fetchFromString(
        '<p>Hi, my name is {{ name }}.</p>',
        [
            'name' => $args['name']
        ]
    );
    $response->getBody()->write($str);
    return $response;
});

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

无容器

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' => 'path/to/cache']);

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

// Define named route
$app->get('/hello/{name}', function ($request, $response, $args) {
    $view = Twig::fromRequest($request);
    return $view->render($response, 'profile.html', [
        'name' => $args['name']
    ]);
})->setName('profile');

// Render from string
$app->get('/hi/{name}', function ($request, $response, $args) {
    $view = Twig::fromRequest($request);
    $str = $view->fetchFromString(
        '<p>Hi, my name is {{ name }}.</p>',
        [
            'name' => $args['name']
        ]
    );
    $response->getBody()->write($str);
    return $response;
});

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

自定义模板函数

TwigExtension为您的Twig模板提供以下函数

  • url_for() - 返回给定路由的URL。例如:/hello/world
  • full_url_for() - 返回给定路由的URL。例如:http://www.example.com/hello/world
  • is_current_url() - 如果提供的路由名称和参数对当前路径有效,则返回true。
  • current_url() - 返回当前路径,带有或没有查询字符串。
  • get_uri() - 从传入的ServerRequestInterface对象返回UriInterface对象
  • base_path() - 返回基本路径。

您可以使用url_for生成到任何Slim应用程序命名路由的完整URL,并使用is_current_url确定是否需要将链接标记为活动状态,如本示例Twig模板所示

{% extends "layout.html" %}

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

测试

要执行测试套件,您需要克隆存储库并安装依赖项。

$ git clone https://github.com/slimphp/Twig-View
$ composer install
$ composer test

贡献

有关详细信息,请参阅CONTRIBUTING

安全

如果您发现任何安全相关的问题,请通过security@slimframework.com发送电子邮件,而不是使用问题跟踪器。

致谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件