PHPackages                             yiisoft/yii-view-renderer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. yiisoft/yii-view-renderer

ActiveLibrary[Framework](/categories/framework)

yiisoft/yii-view-renderer
=========================

PSR-7 compatible view renderer

7.4.1(2mo ago)1762.8k↑15.1%10[3 issues](https://github.com/yiisoft/yii-view-renderer/issues)[1 PRs](https://github.com/yiisoft/yii-view-renderer/pulls)8BSD-3-ClausePHPPHP 8.1 - 8.5CI passing

Since Jul 5Pushed 2mo ago13 watchersCompare

[ Source](https://github.com/yiisoft/yii-view-renderer)[ Packagist](https://packagist.org/packages/yiisoft/yii-view-renderer)[ Docs](https://www.yiiframework.com/)[ GitHub Sponsors](https://github.com/sponsors/yiisoft)[ OpenCollective](https://opencollective.com/yiisoft)[ RSS](/packages/yiisoft-yii-view-renderer/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (48)Versions (25)Used By (8)

 [ ![Yii](https://camo.githubusercontent.com/8317c17418b39410a660f5149071d26c5023c0d5fb2b7ebb771324812f666d73/68747470733a2f2f796969736f66742e6769746875622e696f2f646f63732f696d616765732f7969695f6c6f676f2e737667) ](https://github.com/yiisoft)

Yii View Renderer
=================

[](#yii-view-renderer)

[![Latest Stable Version](https://camo.githubusercontent.com/b5ee9b28d5f01f1fd0af43b24437f6594b3b2792dd2f5243e82b3966e4374c4e/68747470733a2f2f706f7365722e707567782e6f72672f796969736f66742f7969692d766965772d72656e64657265722f76)](https://packagist.org/packages/yiisoft/yii-view-renderer)[![Total Downloads](https://camo.githubusercontent.com/34314985f6c59ab23be86ba3243a4e0119ba03b4a137edec7510eb4715909479/68747470733a2f2f706f7365722e707567782e6f72672f796969736f66742f7969692d766965772d72656e64657265722f646f776e6c6f616473)](https://packagist.org/packages/yiisoft/yii-view-renderer)[![Build status](https://github.com/yiisoft/yii-view-renderer/actions/workflows/build.yml/badge.svg)](https://github.com/yiisoft/yii-view-renderer/actions/workflows/build.yml)[![Code Coverage](https://camo.githubusercontent.com/3a09082057615a8e7279bfe782ac98337b92fed89a1713410306236ad5005f70/68747470733a2f2f636f6465636f762e696f2f67682f796969736f66742f7969692d766965772d72656e64657265722f67726170682f62616467652e7376673f746f6b656e3d57425631335244495058)](https://codecov.io/gh/yiisoft/yii-view-renderer)[![Mutation testing badge](https://camo.githubusercontent.com/f65a272d72a8d5db94104e470a03cab0b5553adf823fa5c89aacce88f9bbcc4e/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f7374796c653d666c61742675726c3d687474707325334125324625324662616467652d6170692e737472796b65722d6d757461746f722e696f2532466769746875622e636f6d253246796969736f66742532467969692d766965772d72656e64657265722532466d6173746572)](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/yii-view-renderer/master)[![static analysis](https://github.com/yiisoft/yii-view-renderer/workflows/static%20analysis/badge.svg)](https://github.com/yiisoft/yii-view-renderer/actions?query=workflow%3A%22static+analysis%22)[![type-coverage](https://camo.githubusercontent.com/3f01d3c20cabc92c579d606dda67ac1e3257ebd7f3cc6c0a592545c370890b04/68747470733a2f2f73686570686572642e6465762f6769746875622f796969736f66742f7969692d766965772d72656e64657265722f636f7665726167652e737667)](https://shepherd.dev/github/yiisoft/yii-view-renderer)

The package is an extension of the [Yii View](https://github.com/yiisoft/view/) rendering library. It adds WEB-specific functionality and compatibility with [PSR-7](https://www.php-fig.org/psr/psr-7/) interfaces.

Requirements
------------

[](#requirements)

- PHP 8.1 - 8.5.

Installation
------------

[](#installation)

The package could be installed with [Composer](https://getcomposer.org):

```
composer require yiisoft/yii-view-renderer
```

General usage
-------------

[](#general-usage)

There are two ways to render a view:

- Return an instance of `Psr\Http\Message\ResponseInterface` with deferred rendering.
- Render immediately and return the rendered result as a string.

### Rendering result as a PSR-7 response

[](#rendering-result-as-a-psr-7-response)

You can get an instance of a response with deferred rendering as follows:

```
/**
 * @var \Psr\Http\Message\ResponseFactoryInterface $responseFactory
 * @var \Psr\Http\Message\StreamFactoryInterface $streamFactory
 * @var \Yiisoft\Aliases\Aliases $aliases
 * @var \Yiisoft\View\WebView $webView
 */

$viewRenderer = new \Yiisoft\Yii\View\Renderer\WebViewRenderer(
    $responseFactory,
    $streamFactory,
    $aliases,
    $webView,
    '/path/to/views', // Full path to the directory of view templates or its alias.
    'layouts/main.php', // Default is null, which means not to use a layout.
);

// Rendering a view with a layout.
$response = $viewRenderer->render('site/page', [
    'parameter-name' => 'parameter-value',
]);
```

The rendering will be performed directly when calling the `getBody()` method of the response. If a layout is set, but you need to render a view without the layout, you can use an immutable setter `withLayout()`:

```
$viewRenderer = $viewRenderer->withLayout(null);

// Rendering a view without a layout.
$response = $viewRenderer->render('site/page', [
    'parameter-name' => 'parameter-value',
]);
```

Or use `renderPartial()` method, which will call `withLayout(null)`:

```
// Rendering a view without a layout.
$response = $viewRenderer->renderPartial('site/page', [
    'parameter-name' => 'parameter-value',
]);
```

### Rendering result as a string

[](#rendering-result-as-a-string)

To render immediately and return the rendering result as a string, use `renderAsString()` and `renderPartialAsString()` methods:

```
// Rendering a view with a layout.
$result = $viewRenderer->renderAsString('site/page', [
    'parameter-name' => 'parameter-value',
]);

// Rendering a view without a layout.
$result = $viewRenderer->renderPartialAsString('site/page', [
    'parameter-name' => 'parameter-value',
]);
```

### Change view templates path

[](#change-view-templates-path)

You can change view templates path in runtime as follows:

```
$viewRenderer = $viewRenderer->withViewPath('/new/path/to/views');
```

You can specify full path to the views directory or its alias. For more information about path aliases, see description of the [yiisoft/aliases](https://github.com/yiisoft/aliases) package.

### Use in the controller

[](#use-in-the-controller)

If the view renderer is used in a controller, you can either specify controller name explicitly using `withControllerName()` or determine name automatically by passing a controller instance to `withController()`. In this case the name is determined as follows:

```
App\Controller\FooBar\BazController -> foo-bar/baz
App\Controllers\FooBar\BazController -> foo-bar/baz
App\AllControllers\MyController\FooBar\BazController -> foo-bar/baz
App\AllControllers\MyController\BazController -> baz
Path\To\File\BlogController -> blog

```

With this approach, you do not need to specify the directory name each time when rendering a view template:

```
use Psr\Http\Message\ResponseInterface;
use Yiisoft\Yii\View\Renderer\WebViewRenderer;

class SiteController
{
    private WebViewRenderer $viewRenderer;

    public function __construct(WebViewRenderer $viewRenderer)
    {
        // Specify the name of the controller:
        $this->viewRenderer = $viewRenderer->withControllerName('site');
        // or specify an instance of the controller:
        //$this->viewRenderer = $viewRenderer->withController($this);
    }

    public function index(): ResponseInterface
    {
        return $this->viewRenderer->render('index');
    }

    public function contact(): ResponseInterface
    {
        // Some actions.
        return $this->viewRenderer->render('contact', [
            'parameter-name' => 'parameter-value',
        ]);
    }
}
```

This is very convenient if there are many methods (actions) in the controller.

### Injection of additional data to the views

[](#injection-of-additional-data-to-the-views)

In addition to parameters passed directly when rendering the view template, you can set extra parameters that will be available in all views. In order to do it you need a class implementing at least one of the injection interfaces:

```
use Yiisoft\Yii\View\Renderer\CommonParametersInjectionInterface;
use Yiisoft\Yii\View\Renderer\LayoutParametersInjectionInterface;

final class MyParametersInjection implements
    CommonParametersInjectionInterface,
    LayoutParametersInjectionInterface
{
    // Pass both to view template and to layout
    public function getCommonParameters(): array
    {
        return [
            'common-parameter-name' => 'common-parameter-value',
        ];
    }

    // Pass only to layout
    public function getLayoutParameters(): array
    {
        return [
            'layout-parameter-name' => 'layout-parameter-value',
        ];
    }
}
```

Link tags and meta tags should be organized in the same way.

```
use Yiisoft\Html\Html;
use Yiisoft\View\WebView;
use Yiisoft\Yii\View\Renderer\LinkTagsInjectionInterface;
use Yiisoft\Yii\View\Renderer\MetaTagsInjectionInterface;

final class MyTagsInjection implements
    LinkTagsInjectionInterface,
    MetaTagsInjectionInterface
{
    public function getLinkTags(): array
    {
        return [
            Html::link()->toCssFile('/main.css'),
            'favicon' => Html::link('/myicon.png', [
                'rel' => 'icon',
                'type' => 'image/png',
            ]),
            'themeCss' => [
                '__position' => WebView::POSITION_END,
                Html::link()->toCssFile('/theme.css'),
            ],
            'userCss' => [
                '__position' => WebView::POSITION_BEGIN,
                'rel' => 'stylesheet',
                'href' => '/user.css',
            ],
        ];
    }

    public function getMetaTags(): array
    {
        return [
            Html::meta()
                ->name('http-equiv')
                ->content('public'),
            'noindex' => Html::meta()
                ->name('robots')
                ->content('noindex'),
            [
                'name' => 'description',
                'content' => 'This website is about funny raccoons.',
            ],
            'keywords' => [
                'name' => 'keywords',
                'content' => 'yii,framework',
            ],
        ];
    }
}
```

You can pass instances of these classes as the sixth optional parameter to the constructor when creating a view renderer, or use the `withInjections()` and `withAddedInjections` methods.

```
$parameters = new MyParametersInjection();
$tags = new MyTagsInjection();

$viewRenderer = $viewRenderer->withInjections($parameters, $tags);
// Or append it:
$viewRenderer = $viewRenderer->withAddedInjections($parameters, $tags);
```

The parameters passed to `render()` method have more priority and will overwrite the injected content parameters if their names match.

#### Injections lazy loading

[](#injections-lazy-loading)

You can use lazy loading for injections. Injections will be created by container that implements `Yiisoft\Yii\View\Renderer\InjectionContainerInterface`. Out of the box, it is available in `InjectionContainer` that is based on PSR-11 compatible container.

1. Add injection container to `WebViewRenderer` constructor:

```
use Yiisoft\Yii\View\Renderer\WebViewRenderer;
use Yiisoft\Yii\View\Renderer\InjectionContainer\InjectionContainer;

/**
 * @var Psr\Container\ContainerInterface $container
 */

$viewRenderer = new WebViewRenderer(
    injectionContainer: new InjectionContainer($container)
)
```

2. Use injection class names instead of instances.

```
$viewRenderer->withInjections(MyParametersInjection::class, MyTagsInjection::class);
```

### Localize view file

[](#localize-view-file)

You can set a specific locale that will be used to localize view files with `withLocale()` method:

```
$viewRenderer = $viewRenderer->withLocale('de_DE');
```

For more information about localization, see at the [localization](https://github.com/yiisoft/view/blob/master/docs/guide/en/basic-functionality.md#localization) section in [yiisoft/view](https://github.com/yiisoft/view) package.

### [Yii Config](https://github.com/yiisoft/config) parameters

[](#yii-config-parameters)

```
'yiisoft/yii-view-renderer' => [
    // The full path to the directory of views or its alias.
    // If null, relative view paths in `WebViewRenderer::render()` is not available.
    'viewPath' => null,

    // The full path to the layout file to be applied to views.
    // If null, the layout will not be applied.
    'layout' => null,

     // The injection instances or class names.
    'injections' => [],
],
```

Documentation
-------------

[](#documentation)

- [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
-------

[](#license)

The Yii View Renderer is free software. It is 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
-------------------

[](#support-the-project)

[![Open Collective](https://camo.githubusercontent.com/a2b15f8e2268d4e3842e00d41ff7a57cce2ad8bd8d8769c5dc4fa05a546a4f62/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4f70656e253230436f6c6c6563746976652d73706f6e736f722d3765616466313f6c6f676f3d6f70656e253230636f6c6c656374697665266c6f676f436f6c6f723d376561646631266c6162656c436f6c6f723d353535353535)](https://opencollective.com/yiisoft)

Follow updates
--------------

[](#follow-updates)

[![Official website](https://camo.githubusercontent.com/d6b0929173e28cc627430d2519ca1853466a70f37395877eaf4820cb3e1e1909/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f506f77657265645f62792d5969695f4672616d65776f726b2d677265656e2e7376673f7374796c653d666c6174)](https://www.yiiframework.com/)[![Twitter](https://camo.githubusercontent.com/d077c362ac639792171af8bc002ee827816733dfc0925f70b557e6d151022226/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f747769747465722d666f6c6c6f772d3144413146323f6c6f676f3d74776974746572266c6f676f436f6c6f723d314441314632266c6162656c436f6c6f723d3535353535353f7374796c653d666c6174)](https://twitter.com/yiiframework)[![Telegram](https://camo.githubusercontent.com/4e38dd12535575c39c65bea7119b95e663abb2d1f4e3d669a27bbda07ef603f0/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f74656c656772616d2d6a6f696e2d3144413146323f7374796c653d666c6174266c6f676f3d74656c656772616d)](https://t.me/yii3en)[![Facebook](https://camo.githubusercontent.com/48204e301b34b29b0815854544f04c337fc0692096cab35e9a1f8c53a42c2307/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f66616365626f6f6b2d6a6f696e2d3144413146323f7374796c653d666c6174266c6f676f3d66616365626f6f6b266c6f676f436f6c6f723d666666666666)](https://www.facebook.com/groups/yiitalk)[![Slack](https://camo.githubusercontent.com/1a3645ba1c97e6684d0349bc478201e1621ba0d3efad516d81035364d442bad7/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f736c61636b2d6a6f696e2d3144413146323f7374796c653d666c6174266c6f676f3d736c61636b)](https://yiiframework.com/go/slack)

###  Health Score

62

—

FairBetter than 99% of packages

Maintenance82

Actively maintained with recent releases

Popularity42

Moderate usage in the ecosystem

Community34

Small or concentrated contributor base

Maturity79

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~90 days

Recently: every ~133 days

Total

20

Last Release

61d ago

Major Versions

2.0.1 → 3.0.02021-09-18

3.0.0 → 4.0.02021-10-21

4.0.3 → 5.0.02022-07-23

5.0.1 → 6.0.02023-02-16

6.1.1 → 7.0.02024-06-22

PHP version history (4 changes)1.0.0PHP ^7.4|^8.0

5.0.1PHP ^8.0

7.2.0PHP ^8.1

7.3.1PHP 8.1 - 8.5

### Community

Maintainers

![](https://www.gravatar.com/avatar/fc29e4e7068a00fe9b9db37b8aadda1db6020adcacef810461e47b99c2b150e6?d=identicon)[samdark](/maintainers/samdark)

![](https://www.gravatar.com/avatar/53e5ee1dedd50f71e4aeeac2929f786cdfb400359d4776e6cd806388d0d5df2c?d=identicon)[vjik](/maintainers/vjik)

---

Top Contributors

[![vjik](https://avatars.githubusercontent.com/u/525501?v=4)](https://github.com/vjik "vjik (66 commits)")[![samdark](https://avatars.githubusercontent.com/u/47294?v=4)](https://github.com/samdark "samdark (50 commits)")[![xepozz](https://avatars.githubusercontent.com/u/6815714?v=4)](https://github.com/xepozz "xepozz (16 commits)")[![devanych](https://avatars.githubusercontent.com/u/20116244?v=4)](https://github.com/devanych "devanych (14 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (14 commits)")[![terabytesoftw](https://avatars.githubusercontent.com/u/42547589?v=4)](https://github.com/terabytesoftw "terabytesoftw (7 commits)")[![thenotsoft](https://avatars.githubusercontent.com/u/44147615?v=4)](https://github.com/thenotsoft "thenotsoft (4 commits)")[![rustamwin](https://avatars.githubusercontent.com/u/16498265?v=4)](https://github.com/rustamwin "rustamwin (3 commits)")[![luizcmarin](https://avatars.githubusercontent.com/u/67489841?v=4)](https://github.com/luizcmarin "luizcmarin (3 commits)")[![StyleCIBot](https://avatars.githubusercontent.com/u/11048387?v=4)](https://github.com/StyleCIBot "StyleCIBot (3 commits)")[![kamarton](https://avatars.githubusercontent.com/u/9432032?v=4)](https://github.com/kamarton "kamarton (2 commits)")[![Fantom409](https://avatars.githubusercontent.com/u/14968877?v=4)](https://github.com/Fantom409 "Fantom409 (1 commits)")[![roxblnfk](https://avatars.githubusercontent.com/u/4152481?v=4)](https://github.com/roxblnfk "roxblnfk (1 commits)")[![sankaest](https://avatars.githubusercontent.com/u/21160342?v=4)](https://github.com/sankaest "sankaest (1 commits)")[![viktorprogger](https://avatars.githubusercontent.com/u/7670669?v=4)](https://github.com/viktorprogger "viktorprogger (1 commits)")[![arogachev](https://avatars.githubusercontent.com/u/8326201?v=4)](https://github.com/arogachev "arogachev (1 commits)")[![mj4444ru](https://avatars.githubusercontent.com/u/9354685?v=4)](https://github.com/mj4444ru "mj4444ru (1 commits)")

---

Tags

hacktoberfestviewyii3viewyiirenderer

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm, Rector

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/yiisoft-yii-view-renderer/health.svg)

```
[![Health](https://phpackages.com/badges/yiisoft-yii-view-renderer/health.svg)](https://phpackages.com/packages/yiisoft-yii-view-renderer)
```

###  Alternatives

[cakephp/cakephp

The CakePHP framework

8.8k18.5M1.6k](/packages/cakephp-cakephp)[yiisoft/yii-dataview

Yii data displaying widgets

4252.6k1](/packages/yiisoft-yii-dataview)[yiisoft/yii-middleware

Yii Middleware

21151.3k1](/packages/yiisoft-yii-middleware)[slim/php-view

Render PHP view scripts into a PSR-7 Response object.

2739.7M95](/packages/slim-php-view)[yiisoft/app

Yii3 web application template

35512.3k](/packages/yiisoft-app)[neos/flow

Flow Application Framework

862.0M451](/packages/neos-flow)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
