yiisoft/view
Composer 安装命令:
composer require yiisoft/view
包简介
Yii View Rendering Library
README 文档
README
Yii View Rendering Library
This library provides templates rendering abstraction supporting layout-view-subview hierarchy, custom renderers with PHP-based as default, and more. It's used in Yii Framework but is usable separately.
Requirements
- PHP 8.1 - 8.5.
Installation
The package could be installed with Composer:
composer require yiisoft/view
General usage
The package provides two use cases for managing view templates:
- Basic functionality for use in any environment.
- Advanced functionality for use in a web environment.
State of View and WebView services
While being immutable and, by itself, stateless, both View and WebView services have sets of stateful and mutable
data.
View service:
- parameters,
- blocks,
- theme,
- locale.
WebView service:
- parameters,
- blocks,
- theme,
- locale,
- title,
- meta and link tags,
- JS/CSS strings,
- JS/CSS files.
The state of View and WebView isn't cloned when the services are cloned. So when
using with*(), both new and old instances are sharing the same set of stateful mutable data. It allows, for example,
to get WebView via type-hinting in a controller and change context path:
final class BlogController { private WebView $view; public function __construct (WebView $view) { $this->view = $view->withContextPath(__DIR__.'/views'); } }
and then register CSS in a widget:
final class LastPosts extends Widget { private WebView $view; public function __construct (WebView $view) { $this->view = $view; } protected function run(): string { ... $this->view->registerCss('.lastPosts { background: #f1f1f1; }'); ... } }
Locale state
You can change the locale by using setLocale(), which will be applied to all other instances that used current state
including existing ones. If you need to change the locale only for a single instance, you can use the immutable
withLocale() method. Locale will be applied to all views rendered within views with render() calls.
Example with mutable method:
final class LocaleMiddleware implements MiddlewareInterface { ... private WebView $view; ... public function __construct ( ... WebView $view ... ) { $this->view = $view; } public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { ... $this->view->setLocale($locale); ... } }
Example with immutable method:
final class BlogController { private WebView $view; public function __construct (WebView $view) { $this->view = $view; } public function index() { return $this->view->withLocale('es')->render('index'); } }
Reset state
To get a deep cloned View or WebView with a reset state use withClearedState():
$view = $view->withClearedState();
Deep clone
To get a deep cloned View or WebView, including state cloning, use deepClone() method:
$view = $view->deepClone();
Extensions
- Yii View Renderer - a wrapper that's used in Yii Framework. Adds extra functionality for a web environment and compatibility with PSR-7 interfaces.
- yiisoft/view-twig - an extension that provides a view renderer that will allow you to use the Twig view template engine, instead of the default PHP renderer.
Documentation
- Guide: English, Português - Brasil
- Internals
If you need help or have a question, the Yii Forum is a good place for that. You may also check out other Yii Community Resources.
License
The Yii View Rendering Library is free software. It's released under the terms of the BSD License.
Please see LICENSE for more information.
Maintained by Yii Software.
Support the project
Follow updates
yiisoft/view 适用场景与选型建议
yiisoft/view 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 415.74k 次下载、GitHub Stars 达 56, 最近一次更新时间为 2018 年 08 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「view」 「yii」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yiisoft/view 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yiisoft/view 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yiisoft/view 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
Illuminate View for Morningmedley.
Priveate for SkeekS CMS
Blade template for Kirby
View5 is a version of Blade Templates for the Mvc5 Framework
统计信息
- 总下载量: 415.74k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 57
- 点击次数: 22
- 依赖项目数: 56
- 推荐数: 2
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2018-08-08