slim/twig-view
Composer 安装命令:
composer require slim/twig-view
包简介
Slim Framework 4 view helper built on top of the Twig 3 templating component
关键字:
README 文档
README
This is a Slim Framework view helper built on top of the Twig templating component. You can use this component to create and render templates in your Slim Framework application.
Install
Via Composer
composer require slim/twig-view
Requires Slim Framework 4, Twig 3 and PHP 7.4 or newer.
Usage
With DI Container
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(); // Set view in Container $container->set(Twig::class, function() { return Twig::create(__DIR__ . '/../templates', ['cache' => 'path/to/cache']); }); // Create App from container $app = AppFactory::createFromContainer($container); // Add Twig-View Middleware $app->add(TwigMiddleware::create($app, $container->get(Twig::class))); // Add other middleware $app->addRoutingMiddleware(); $app->addErrorMiddleware(true, true, true); // Render from template file templates/profile.html.twig $app->get('/hello/{name}', function ($request, $response, $args) { $viewData = [ 'name' => $args['name'], ]; $twig = $this->get(Twig::class); return $twig->render($response, 'profile.html.twig', $viewData); })->setName('profile'); // Render from string $app->get('/hi/{name}', function ($request, $response, $args) { $viewData = [ 'name' => $args['name'], ]; $twig = $this->get(Twig::class); $str = $twig->fetchFromString('<p>Hi, my name is {{ name }}.</p>', $viewData); $response->getBody()->write($str); return $response; }); // Run app $app->run();
Without DI container
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.twig', [ '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();
Custom template functions
TwigExtension provides these functions to your Twig templates:
url_for()- returns the URL for a given route. e.g.: /hello/worldfull_url_for()- returns the URL for a given route. e.g.: https://www.example.com/hello/worldis_current_url()- returns true is the provided route name and parameters are valid for the current path.current_url()- returns the current path, with or without the query string.get_uri()- returns theUriInterfaceobject from the incomingServerRequestInterfaceobjectbase_path()- returns the base path.
You can use url_for to generate complete URLs to any Slim application named route and use is_current_url to determine if you need to mark a link as active as shown in this example Twig template:
<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>
Tests
To execute the test suite, you'll need to clone the repository and install the dependencies.
$ git clone https://github.com/slimphp/Twig-View
$ composer install
$ composer test
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email security@slimframework.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
slim/twig-view 适用场景与选型建议
slim/twig-view 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.67M 次下载、GitHub Stars 达 371, 最近一次更新时间为 2015 年 03 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「twig」 「template」 「view」 「slim」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 slim/twig-view 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 slim/twig-view 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 slim/twig-view 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
Shoot aims to make providing data to your templates more manageable
This Symfony bundle integrates PhpSpreadsheet into Symfony using Twig.
A Twig extension to insert css as inline styles with a tag
Caching and compression for Twig assets (JavaScript and CSS).
Twig extensions for Tracy Debugger
统计信息
- 总下载量: 8.67M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 400
- 点击次数: 19
- 依赖项目数: 275
- 推荐数: 11
其他信息
- 授权协议: MIT
- 更新时间: 2015-03-14