https://github.com/yiisoft/view
Yii view rendering library
https://github.com/yiisoft/view
hacktoberfest mvc view widgets yii3
Last synced: about 1 month ago
JSON representation
Yii view rendering library
- Host: GitHub
- URL: https://github.com/yiisoft/view
- Owner: yiisoft
- License: bsd-3-clause
- Created: 2018-08-08T08:12:43.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2024-10-14T08:15:21.000Z (7 months ago)
- Last Synced: 2024-10-29T14:36:08.423Z (7 months ago)
- Topics: hacktoberfest, mvc, view, widgets, yii3
- Language: PHP
- Homepage: https://www.yiiframework.com/
- Size: 626 KB
- Stars: 56
- Watchers: 21
- Forks: 44
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
Yii View Rendering Library
[](https://packagist.org/packages/yiisoft/view)
[](https://packagist.org/packages/yiisoft/view)
[](https://github.com/yiisoft/view/actions/workflows/build.yml)
[](https://codecov.io/gh/yiisoft/view)
[](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/view/master)
[](https://github.com/yiisoft/view/actions/workflows/static.yml?query=branch%3Amaster)
[](https://shepherd.dev/github/yiisoft/view)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](https://www.yiiframework.com/) but is usable separately.## Requirements
- PHP 8.1 or higher.
## Installation
The package could be installed with [Composer](https://getcomposer.org):
```shell
composer require yiisoft/view
```## General usage
The package provides two use cases for managing view templates:
- [Basic functionality](docs/guide/en/basic-functionality.md) for use in any environment.
- Advanced functionality for [use in a web environment](docs/guide/en/use-in-web-environment.md).### 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:```php
final class BlogController {
private WebView $view;
public function __construct (WebView $view) {
$this->view = $view->withContextPath(__DIR__.'/views');
}
}
```and then register CSS in a widget:
```php
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:
```php
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:
```php
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()`:
```php
$view = $view->withClearedState();
```#### Deep clone
To get a deep cloned `View` or `WebView`, including state cloning, use `deepClone()` method:
```php
$view = $view->deepClone();
```## Extensions
- [Yii View Renderer](https://github.com/yiisoft/yii-view-renderer) - a wrapper that's used in
[Yii Framework](https://www.yiiframework.com/).
Adds extra functionality for a web environment and compatibility
with [PSR-7](https://www.php-fig.org/psr/psr-7) interfaces.
- [yiisoft/view-twig](https://github.com/yiisoft/view-twig) - an extension that provides a view renderer that will
allow you to use the [Twig](https://twig.symfony.com) view template engine, instead of the default PHP renderer.## Documentation
- Guide: [English](docs/guide/en/README.md), [Português - Brasil](docs/guide/pt-BR/README.md)
- [Internals](docs/internals.md)If you need help or have a question, the [Yii Forum](https://forum.yiiframework.com/c/yii-3-0/63) is a good place for that.
You may also check out other [Yii Community Resources](https://www.yiiframework.com/community).## License
The Yii View Rendering Library is free software. It's released under the terms of the BSD License.
Please see [`LICENSE`](./LICENSE.md) for more information.Maintained by [Yii Software](https://www.yiiframework.com/).
### Support the project
[](https://opencollective.com/yiisoft)
### Follow updates
[](https://www.yiiframework.com/)
[](https://twitter.com/yiiframework)
[](https://t.me/yii3en)
[](https://www.facebook.com/groups/yiitalk)
[](https://yiiframework.com/go/slack)