Yii View Rendering Library

4.0.0 2021-10-25 09:17 UTC

This package is auto-updated.

Last update: 2022-01-12 15:45:57 UTC



Yii View Rendering Library

Latest Stable Version Total Downloads Build Status Code Coverage Scrutinizer Quality Score Mutation testing badge static analysis type-coverage

This library provides templates rendering abstraction supporting layout-view-subview hierarchy, custom renderers with PHP-based as default and more. It is used in Yii Framework but is supposed to be usable separately.


  • PHP 7.4 or higher.


The package could be installed via composer:

composer require yiisoft/view --prefer-dist

General usage

The package provides two use cases for managing view templates:

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.

WebView service:

  • parameters,
  • blocks,
  • title,
  • meta and link tags,
  • JS/CSS strings,
  • JS/CSS files.

The state of View and WebView is not 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; }');

To get a deep cloned View or WebView use withClearedState():

$view = $view->withClearedState();


  • yiisoft/yii-view - a wrapper that is used in Yii Framework. Adds additional 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.


Unit testing

The package is tested with PHPUnit. To run tests:


Mutation testing

The package tests are checked with Infection mutation framework with Infection Static Analysis Plugin. To run it:


Static analysis

The code is statically analyzed with Psalm. To run static analysis:



The Yii View Rendering Library is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.

Maintained by Yii Software.

Support the project

Open Collective

Follow updates

Official website Twitter Telegram Facebook Slack