PHPackages                             rossaddison/yii-view - 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. [Templating &amp; Views](/categories/templating)
4. /
5. rossaddison/yii-view

ActiveLibrary[Templating &amp; Views](/categories/templating)

rossaddison/yii-view
====================

Yii View Extension

0219PHP

Since Jun 16Pushed 2y agoCompare

[ Source](https://github.com/rossaddison/yii-view)[ Packagist](https://packagist.org/packages/rossaddison/yii-view)[ RSS](/packages/rossaddison-yii-view/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependenciesVersions (1)Used By (0)

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

Yii View Extension
==================

[](#yii-view-extension)

[![Latest Stable Version](https://camo.githubusercontent.com/f3af4249350cf319de29a6cf855b9eb1ac2250026f55083d9431672f862fbb29/68747470733a2f2f706f7365722e707567782e6f72672f796969736f66742f7969692d766965772f762f737461626c652e706e67)](https://packagist.org/packages/yiisoft/yii-view)[![Total Downloads](https://camo.githubusercontent.com/a56c82217b12aff0b966be5c11cec69da26cc4e6bacbb2391998aae5bcb4dcdb/68747470733a2f2f706f7365722e707567782e6f72672f796969736f66742f7969692d766965772f646f776e6c6f6164732e706e67)](https://packagist.org/packages/yiisoft/yii-view)[![Build status](https://github.com/yiisoft/yii-view/workflows/build/badge.svg)](https://github.com/yiisoft/yii-view/actions?query=workflow%3Abuild)[![Code Coverage](https://camo.githubusercontent.com/7da55a65320bb33b0f218066689b00d88cb35317837992176bd65c638e2b3d6f/68747470733a2f2f636f6465636f762e696f2f67682f796969736f66742f7969692d766965772f67726170682f62616467652e7376673f746f6b656e3d57425631335244495058)](https://codecov.io/gh/yiisoft/yii-view)[![Mutation testing badge](https://camo.githubusercontent.com/6bb6c45ebf9a6c68ea5c6aa1d337764ecf9f824b36e49e0a667ee2e90f4b758d/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f7374796c653d666c61742675726c3d687474707325334125324625324662616467652d6170692e737472796b65722d6d757461746f722e696f2532466769746875622e636f6d253246796969736f66742532467969692d766965772532466d6173746572)](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/yii-view/master)[![static analysis](https://github.com/yiisoft/yii-view/workflows/static%20analysis/badge.svg)](https://github.com/yiisoft/yii-view/actions?query=workflow%3A%22static+analysis%22)[![type-coverage](https://camo.githubusercontent.com/2a131668075431f562a88aae64395f84af733f1a8ee2fc935630b0a58b7d3211/68747470733a2f2f73686570686572642e6465762f6769746875622f796969736f66742f7969692d766965772f636f7665726167652e737667)](https://shepherd.dev/github/yiisoft/yii-view)

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

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

[](#requirements)

- PHP 8.0 or higher.

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

[](#installation)

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

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

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

[](#general-usage)

There are two ways to render a view:

- Return an instance of the `Yiisoft\DataResponse\DataResponse` class 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)

The `Yiisoft\DataResponse\DataResponse` class is an implementation of the `Psr\Http\Message\ResponseInterface`. For more information about this class, see the [yiisoft/data-response](https://github.com/yiisoft/data-response) package. You can get an instance of a response with deferred rendering as follows:

```
/**
 * @var \Yiisoft\Aliases\Aliases $aliases
 * @var \Yiisoft\DataResponse\DataResponseFactoryInterface $dataResponseFactory
 * @var \Yiisoft\View\WebView $webView
 */

$viewRenderer = new \Yiisoft\Yii\View\ViewRenderer(
    $dataResponseFactory,
    $aliases,
    $webView,
    '/path/to/views', // Full path to the directory of view templates or its alias.
    'layouts/main', // 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 `getBody()` or `getData()` methods of the `Yiisoft\DataResponse\DataResponse`. 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
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\ViewRenderer;

class SiteController
{
    private ViewRenderer $viewRenderer;

    public function __construct(ViewRenderer $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\CommonParametersInjectionInterface;
use Yiisoft\Yii\View\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\LinkTagsInjectionInterface;
use Yiisoft\Yii\View\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\InjectionContainerInterface`. Out of the box, it is available in `InjectionContainer` that is based on PSR-11 compatible container.

1. Add injection container to `ViewRenderer` constructor:

```
use Yiisoft\Yii\View\ViewRenderer;
use Yiisoft\Yii\View\InjectionContainer\InjectionContainer;

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

$viewRenderer = new ViewRenderer(
    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.

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

16

—

LowBetter than 4% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity18

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/79f16141a5b619c098d6bec3812cd770dfc9b48eacc093f4ef88fbf20e735ec1?d=identicon)[yii2house2house](/maintainers/yii2house2house)

---

Top Contributors

[![samdark](https://avatars.githubusercontent.com/u/47294?v=4)](https://github.com/samdark "samdark (50 commits)")[![vjik](https://avatars.githubusercontent.com/u/525501?v=4)](https://github.com/vjik "vjik (36 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] (12 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)")[![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)")[![rustamwin](https://avatars.githubusercontent.com/u/16498265?v=4)](https://github.com/rustamwin "rustamwin (2 commits)")[![kamarton](https://avatars.githubusercontent.com/u/9432032?v=4)](https://github.com/kamarton "kamarton (2 commits)")[![sankaest](https://avatars.githubusercontent.com/u/21160342?v=4)](https://github.com/sankaest "sankaest (1 commits)")[![arogachev](https://avatars.githubusercontent.com/u/8326201?v=4)](https://github.com/arogachev "arogachev (1 commits)")[![roxblnfk](https://avatars.githubusercontent.com/u/4152481?v=4)](https://github.com/roxblnfk "roxblnfk (1 commits)")[![rossaddison](https://avatars.githubusercontent.com/u/8538339?v=4)](https://github.com/rossaddison "rossaddison (1 commits)")[![viktorprogger](https://avatars.githubusercontent.com/u/7670669?v=4)](https://github.com/viktorprogger "viktorprogger (1 commits)")[![mj4444ru](https://avatars.githubusercontent.com/u/9354685?v=4)](https://github.com/mj4444ru "mj4444ru (1 commits)")[![Fantom409](https://avatars.githubusercontent.com/u/14968877?v=4)](https://github.com/Fantom409 "Fantom409 (1 commits)")

### Embed Badge

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

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

###  Alternatives

[limenius/react-bundle

Client and Server-side react rendering in a Symfony Bundle

3861.2M](/packages/limenius-react-bundle)[area17/laravel-auto-head-tags

Laravel Auto Head Tags helps you build the list of head elements for your app

4616.0k](/packages/area17-laravel-auto-head-tags)[jelix/wikirenderer

WikiRenderer is a library to generate HTML or anything else from wiki content.

1712.2k1](/packages/jelix-wikirenderer)[webkinder/sproutset

A Composer package for handling responsive images in Roots Bedrock + Sage + Blade projects.

291.8k](/packages/webkinder-sproutset)[awkwardideas/switchblade

Extended blade directives for laravel

102.1k](/packages/awkwardideas-switchblade)

PHPackages © 2026

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