# View

> View represents a view object in the MVC pattern.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [base](/docs/api/yii/base/index.md) › View

| class | `yii\base\View` |
| --- | --- |
| Extends | [`Component`](/docs/api/yii/base/component/index.md) » [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`DynamicContentAwareInterface`](/docs/api/yii/base/dynamiccontentawareinterface/index.md), [`Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/base/View.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/base/View.php#L31) |

1 subclass:

- [`yii\web\View`](/docs/api/yii/web/view/index.md)

View represents a view object in the MVC pattern.

View provides a set of methods (e.g. [`render()`](/docs/api/yii/base/view/index.md#method-render)) for rendering purpose.

For more details and usage information on View, see the [guide article on views](/docs/guide/structure-views/index.md).

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`Component`](/docs/api/yii/base/component/index.md) |
| [`$blocks`](#property-blocks) | `array` | A list of named output blocks. Default: `null`. | `View` |
| <span id="property-context"></span>`$context` | [`ViewContextInterface`](/docs/api/yii/base/viewcontextinterface/index.md) | The context under which the [`renderFile()`](/docs/api/yii/base/view/index.md#method-renderFile) method is being invoked. Default: `null`. | `View` |
| [`$defaultExtension`](#property-defaultExtension) | `string` | The default view file extension. Default: `'php'`. | `View` |
| <span id="property-dynamicContents"></span>`$dynamicContents` read-only | [`DynamicContentAwareInterface`](/docs/api/yii/base/dynamiccontentawareinterface/index.md)`[]` | Class instances supporting dynamic contents. | `View` |
| <span id="property-dynamicPlaceholders"></span>`$dynamicPlaceholders` | `array` | A list of placeholders. | `View` |
| <span id="property-params"></span>`$params` | `array` | Custom parameters that are shared among view templates. Default: `[]`. | `View` |
| [`$renderers`](#property-renderers) | `array\|null` | A list of available renderers indexed by their corresponding supported file extensions. Default: `null`. | `View` |
| [`$theme`](#property-theme) | [`Theme`](/docs/api/yii/base/theme/index.md)`\|array\|string\|null` | The theme object or the configuration for creating the theme object. Default: `null`. | `View` |
| [`$viewFile`](#property-viewFile) read-only | `string\|bool` | The view file currently being rendered. | `View` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/component/index.md#method-__call) | Calls the named method which is not a class method. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__clone"></span>[`__clone()`](/docs/api/yii/base/component/index.md#method-__clone) | This method is called after the object is created by cloning an existing one. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/component/index.md#method-__get) | Returns the value of a component property. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/component/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/component/index.md#method-__set) | Sets the value of a component property. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/component/index.md#method-__unset) | Sets a component property to be null. | [`Component`](/docs/api/yii/base/component/index.md) |
| [`addDynamicPlaceholder()`](#method-addDynamicPlaceholder) | Adds a placeholder for dynamic content. | `View` |
| [`afterRender()`](#method-afterRender) | This method is invoked right after [`renderFile()`](/docs/api/yii/base/view/index.md#method-renderFile) renders a view file. | `View` |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehaviors"></span>[`attachBehaviors()`](/docs/api/yii/base/component/index.md#method-attachBehaviors) | Attaches a list of behaviors to the component. | [`Component`](/docs/api/yii/base/component/index.md) |
| [`beforeRender()`](#method-beforeRender) | This method is invoked right before [`renderFile()`](/docs/api/yii/base/view/index.md#method-renderFile) renders a view file. | `View` |
| [`beginBlock()`](#method-beginBlock) | Begins recording a block. | `View` |
| [`beginCache()`](#method-beginCache) | Begins fragment caching. | `View` |
| [`beginContent()`](#method-beginContent) | Begins the rendering of content that is to be decorated by the specified view. | `View` |
| [`beginPage()`](#method-beginPage) | Marks the beginning of a page. | `View` |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/component/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/component/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-detachBehaviors"></span>[`detachBehaviors()`](/docs/api/yii/base/component/index.md#method-detachBehaviors) | Detaches all behaviors from the component. | [`Component`](/docs/api/yii/base/component/index.md) |
| [`endBlock()`](#method-endBlock) | Ends recording a block. | `View` |
| [`endCache()`](#method-endCache) | Ends fragment caching. | `View` |
| [`endContent()`](#method-endContent) | Ends the rendering of content. | `View` |
| [`endPage()`](#method-endPage) | Marks the ending of a page. | `View` |
| <span id="method-ensureBehaviors"></span>[`ensureBehaviors()`](/docs/api/yii/base/component/index.md#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | [`Component`](/docs/api/yii/base/component/index.md) |
| [`evaluateDynamicContent()`](#method-evaluateDynamicContent) | Evaluates the given PHP statements. | `View` |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehaviors"></span>[`getBehaviors()`](/docs/api/yii/base/component/index.md#method-getBehaviors) | Returns all behaviors attached to this component. | [`Component`](/docs/api/yii/base/component/index.md) |
| [`getDynamicContents()`](#method-getDynamicContents) | Returns a list of currently active dynamic content class instances. | `View` |
| [`getDynamicPlaceholders()`](#method-getDynamicPlaceholders) | Returns a list of placeholders for dynamic content. This method is used internally to implement the content caching feature. | `View` |
| [`getViewFile()`](#method-getViewFile) |  | `View` |
| <span id="method-hasEventHandlers"></span>[`hasEventHandlers()`](/docs/api/yii/base/component/index.md#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/component/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/component/index.md#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | [`Component`](/docs/api/yii/base/component/index.md) |
| [`init()`](#method-init) | Initializes the view component. | `View` |
| <span id="method-off"></span>[`off()`](/docs/api/yii/base/component/index.md#method-off) | Detaches an existing event handler from this component. | [`Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-on"></span>[`on()`](/docs/api/yii/base/component/index.md#method-on) | Attaches an event handler to an event. | [`Component`](/docs/api/yii/base/component/index.md) |
| [`popDynamicContent()`](#method-popDynamicContent) | Removes a last class instance supporting dynamic contents from a list of currently active dynamic content class instances. | `View` |
| [`pushDynamicContent()`](#method-pushDynamicContent) | Adds a class instance supporting dynamic contents to the end of a list of currently active dynamic content class instances. | `View` |
| [`render()`](#method-render) | Renders a view. | `View` |
| [`renderDynamic()`](#method-renderDynamic) | Renders dynamic content returned by the given PHP statements. | `View` |
| [`renderFile()`](#method-renderFile) | Renders a view file. | `View` |
| [`renderPhpFile()`](#method-renderPhpFile) | Renders a view file as a PHP script. | `View` |
| [`setDynamicPlaceholders()`](#method-setDynamicPlaceholders) | Sets a list of placeholders for dynamic content. This method is used internally to implement the content caching feature. | `View` |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`Component`](/docs/api/yii/base/component/index.md) |
| [`findViewFile()`](#method-findViewFile) protected | Finds the view file based on the given view name. | `View` |
| [`getRequestedViewFile()`](#method-getRequestedViewFile) protected |  | `View` |

## Events

| Event | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="event-EVENT_BEGIN_PAGE"></span>`EVENT_BEGIN_PAGE` | [`Event`](/docs/api/yii/base/event/index.md) | An event that is triggered by [`beginPage()`](/docs/api/yii/base/view/index.md#method-beginPage). Event name: `'beginPage'`. | `View` |
| <span id="event-EVENT_END_PAGE"></span>`EVENT_END_PAGE` | [`Event`](/docs/api/yii/base/event/index.md) | An event that is triggered by [`endPage()`](/docs/api/yii/base/view/index.md#method-endPage). Event name: `'endPage'`. | `View` |
| <span id="event-EVENT_BEFORE_RENDER"></span>`EVENT_BEFORE_RENDER` | [`ViewEvent`](/docs/api/yii/base/viewevent/index.md) | An event that is triggered by [`renderFile()`](/docs/api/yii/base/view/index.md#method-renderFile) right before it renders a view file. Event name: `'beforeRender'`. | `View` |
| <span id="event-EVENT_AFTER_RENDER"></span>`EVENT_AFTER_RENDER` | [`ViewEvent`](/docs/api/yii/base/viewevent/index.md) | An event that is triggered by [`renderFile()`](/docs/api/yii/base/view/index.md#method-renderFile) right after it renders a view file. Event name: `'afterRender'`. | `View` |

## Property details

### $blocks <span id="property-blocks"></span>

```php
public array $blocks = null
```

A list of named output blocks.

The keys are the block names and the values
are the corresponding block content. You can call [`beginBlock()`](/docs/api/yii/base/view/index.md#method-beginBlock) and [`endBlock()`](/docs/api/yii/base/view/index.md#method-endBlock)
to capture small fragments of a view. They can be later accessed somewhere else
through this property.

### $defaultExtension <span id="property-defaultExtension"></span>

```php
public string $defaultExtension = 'php'
```

The default view file extension.

This will be appended to view file names if they don't have file extensions.

### $renderers <span id="property-renderers"></span>

```php
public array|null $renderers = null
```

A list of available renderers indexed by their corresponding supported file extensions.

Each renderer may be a view renderer object or the configuration for creating the renderer object.
For example, the following configuration enables both Smarty and Twig view renderers:

```
[
    'tpl' => ['class' => 'yii\smarty\ViewRenderer'],
    'twig' => ['class' => 'yii\twig\ViewRenderer'],
]
```

If no renderer is available for the given view file, the view file will be treated as a normal PHP
and rendered via [`renderPhpFile()`](/docs/api/yii/base/view/index.md#method-renderPhpFile).

### $theme <span id="property-theme"></span>

```php
public yii\base\Theme|array|string|null $theme = null
```

The theme object or the configuration for creating the theme object.

If not set, it means theming is not enabled.

### $viewFile <span id="property-viewFile"></span>

```php
public string|bool $viewFile
```

The view file currently being rendered.

False if no view file is being
rendered.

**Access**

Read-only (virtual property).

## Method details

### addDynamicPlaceholder() <span id="method-addDynamicPlaceholder"></span>

```php
public function addDynamicPlaceholder(mixed $placeholder, mixed $statements): mixed
```

Adds a placeholder for dynamic content.

This method is used internally to implement the content caching feature.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$placeholder` | `mixed` |  |
| `$statements` | `mixed` | The PHP statements for generating the dynamic content. |

**Returns**

`mixed`

### afterRender() <span id="method-afterRender"></span>

```php
public function afterRender(string $viewFile, array $params, string &$output): mixed
```

This method is invoked right after [`renderFile()`](/docs/api/yii/base/view/index.md#method-renderFile) renders a view file.

The default implementation will trigger the [`EVENT_AFTER_RENDER`](/docs/api/yii/base/view/index.md#event-EVENT_AFTER_RENDER) event.
If you override this method, make sure you call the parent implementation first.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$viewFile` | `string` | The view file being rendered. |
| `$params` | `array` | The parameter array passed to the [`render()`](/docs/api/yii/base/view/index.md#method-render) method. |
| `$output` | `string` | The rendering result of the view file. Updates to this parameter will be passed back and returned by [`renderFile()`](/docs/api/yii/base/view/index.md#method-renderFile). |

**Returns**

`mixed`

### beforeRender() <span id="method-beforeRender"></span>

```php
public function beforeRender(string $viewFile, array $params): bool
```

This method is invoked right before [`renderFile()`](/docs/api/yii/base/view/index.md#method-renderFile) renders a view file.

The default implementation will trigger the [`EVENT_BEFORE_RENDER`](/docs/api/yii/base/view/index.md#event-EVENT_BEFORE_RENDER) event.
If you override this method, make sure you call the parent implementation first.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$viewFile` | `string` | The view file to be rendered. |
| `$params` | `array` | The parameter array passed to the [`render()`](/docs/api/yii/base/view/index.md#method-render) method. |

**Returns**

`bool`: Whether to continue rendering the view file.

### beginBlock() <span id="method-beginBlock"></span>

```php
public function beginBlock(string $id, bool $renderInPlace = false): yii\widgets\Block
```

Begins recording a block.

This method is a shortcut to beginning [`Block`](/docs/api/yii/widgets/block/index.md).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$id` | `string` | The block ID. |
| `$renderInPlace` | `bool` | Whether to render the block content in place. Defaults to false, meaning the captured block will not be displayed. |

**Returns**

[`yii\widgets\Block`](/docs/api/yii/widgets/block/index.md): The Block widget instance

### beginCache() <span id="method-beginCache"></span>

```php
public function beginCache(string $id, array $properties = []): bool
```

Begins fragment caching.

This method will display cached content if it is available.
If not, it will start caching and would expect an [`endCache()`](/docs/api/yii/base/view/index.md#method-endCache)
call to end the cache and save the content into cache.
A typical usage of fragment caching is as follows,

```
if ($this->beginCache($id)) {
    // ...generate content here
    $this->endCache();
}
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$id` | `string` | A unique ID identifying the fragment to be cached. |
| `$properties` | `array` | Initial property values for [`FragmentCache`](/docs/api/yii/widgets/fragmentcache/index.md) |

**Returns**

`bool`: Whether you should generate the content for caching. False if the cached version is available.

### beginContent() <span id="method-beginContent"></span>

```php
public function beginContent(string $viewFile, array $params = []): yii\widgets\ContentDecorator
```

Begins the rendering of content that is to be decorated by the specified view.

This method can be used to implement nested layout. For example, a layout can be embedded
in another layout file specified as '@app/views/layouts/base.php' like the following:

```
<?php $this->beginContent('@app/views/layouts/base.php'); ?>
//...layout content here...
<?php $this->endContent(); ?>
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$viewFile` | `string` | The view file that will be used to decorate the content enclosed by this widget. This can be specified as either the view file path or [path alias](/docs/guide/concept-aliases/index.md). |
| `$params` | `array` | The variables (name => value) to be extracted and made available in the decorative view. |

**Returns**

[`yii\widgets\ContentDecorator`](/docs/api/yii/widgets/contentdecorator/index.md): The ContentDecorator widget instance

**See also**

- [`ContentDecorator`](/docs/api/yii/widgets/contentdecorator/index.md)

### beginPage() <span id="method-beginPage"></span>

```php
public function beginPage(): mixed
```

Marks the beginning of a page.

**Returns**

`mixed`

### endBlock() <span id="method-endBlock"></span>

```php
public function endBlock(): mixed
```

Ends recording a block.

**Returns**

`mixed`

### endCache() <span id="method-endCache"></span>

```php
public function endCache(): mixed
```

Ends fragment caching.

**Returns**

`mixed`

### endContent() <span id="method-endContent"></span>

```php
public function endContent(): mixed
```

Ends the rendering of content.

**Returns**

`mixed`

### endPage() <span id="method-endPage"></span>

```php
public function endPage(): mixed
```

Marks the ending of a page.

**Returns**

`mixed`

### evaluateDynamicContent() <span id="method-evaluateDynamicContent"></span>

```php
public function evaluateDynamicContent(string $statements): mixed
```

Evaluates the given PHP statements.

This method is mainly used internally to implement dynamic content feature.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$statements` | `string` | The PHP statements to be evaluated. |

**Returns**

`mixed`: The return value of the PHP statements.

### getDynamicContents() <span id="method-getDynamicContents"></span>

```php
public function getDynamicContents(): yii\base\DynamicContentAwareInterface[]
```

Returns a list of currently active dynamic content class instances.

**Returns**

[`DynamicContentAwareInterface`](/docs/api/yii/base/dynamiccontentawareinterface/index.md)`[]`: Class instances supporting dynamic contents.

**Since**

2.0.14

### getDynamicPlaceholders() <span id="method-getDynamicPlaceholders"></span>

```php
public function getDynamicPlaceholders(): array
```

Returns a list of placeholders for dynamic content. This method
is used internally to implement the content caching feature.

**Returns**

`array`: A list of placeholders.

### getViewFile() <span id="method-getViewFile"></span>

```php
public function getViewFile(): string|bool
```

**Returns**

`string|bool`: The view file currently being rendered. False if no view file is being rendered.

### init() <span id="method-init"></span>

```php
public function init(): mixed
```

Initializes the view component.

**Returns**

`mixed`

### popDynamicContent() <span id="method-popDynamicContent"></span>

```php
public function popDynamicContent(): mixed
```

Removes a last class instance supporting dynamic contents from a list of currently active
dynamic content class instances.

**Returns**

`mixed`

**Since**

2.0.14

### pushDynamicContent() <span id="method-pushDynamicContent"></span>

```php
public function pushDynamicContent(yii\base\DynamicContentAwareInterface $instance): mixed
```

Adds a class instance supporting dynamic contents to the end of a list of currently active
dynamic content class instances.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$instance` | [`DynamicContentAwareInterface`](/docs/api/yii/base/dynamiccontentawareinterface/index.md) | Class instance supporting dynamic contents. |

**Returns**

`mixed`

**Since**

2.0.14

### render() <span id="method-render"></span>

```php
public function render(string $view, array $params = [], object|null $context = null): string
```

Renders a view.

The view to be rendered can be specified in one of the following formats:

- [path alias](/docs/guide/concept-aliases/index.md) (e.g. "@app/views/site/index");
- absolute path within application (e.g. "//site/index"): the view name starts with double slashes.
  The actual view file will be looked for under the [view path](/docs/api/yii/base/module/index.md#property-viewPath) of the application.
- absolute path within current module (e.g. "/site/index"): the view name starts with a single slash.
  The actual view file will be looked for under the [view path](/docs/api/yii/base/module/index.md#property-viewPath) of the [current module](/docs/api/yii/base/controller/index.md#property-module).
- relative view (e.g. "index"): the view name does not start with `@` or `/`. The corresponding view file will be
  looked for under the [view path](/docs/api/yii/base/viewcontextinterface/index.md#method-getViewPath) of the view `$context`.
  If `$context` is not given, it will be looked for under the directory containing the view currently
  being rendered (i.e., this happens when rendering a view within another view).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$view` | `string` | The view name. |
| `$params` | `array` | The parameters (name-value pairs) that will be extracted and made available in the view file. |
| `$context` | `object\|null` | The context to be assigned to the view and can later be accessed via [`$context`](/docs/api/yii/base/view/index.md#property-context) in the view. If the context implements [`ViewContextInterface`](/docs/api/yii/base/viewcontextinterface/index.md), it may also be used to locate the view file corresponding to a relative view name. |

**Returns**

`string`: The rendering result

**Throws**

- [`ViewNotFoundException`](/docs/api/yii/base/viewnotfoundexception/index.md): if the view file does not exist.
- [`InvalidCallException`](/docs/api/yii/base/invalidcallexception/index.md): if the view cannot be resolved.

**See also**

- [`renderFile()`](/docs/api/yii/base/view/index.md#method-renderFile)

### renderDynamic() <span id="method-renderDynamic"></span>

```php
public function renderDynamic(string $statements): string
```

Renders dynamic content returned by the given PHP statements.

This method is mainly used together with content caching (fragment caching and page caching)
when some portions of the content (called *dynamic content*) should not be cached.
The dynamic content must be returned by some PHP statements.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$statements` | `string` | The PHP statements for generating the dynamic content. |

**Returns**

`string`: The placeholder of the dynamic content, or the dynamic content if there is no active content cache currently. Note that most methods that indirectly modify layout such as registerJS() or registerJSFile() do not work with dynamic rendering.

**See also**

- <https://github.com/yiisoft/yii2/issues/17673>

### renderFile() <span id="method-renderFile"></span>

```php
public function renderFile(string $viewFile, array $params = [], object|null $context = null): string
```

Renders a view file.

If [`$theme`](/docs/api/yii/base/view/index.md#property-theme) is enabled (not null), it will try to render the themed version of the view file as long
as it is available.

The method will call [`FileHelper::localize()`](/docs/api/yii/helpers/basefilehelper/index.md#method-localize) to localize the view file.

If [renderer](/docs/api/yii/base/view/index.md#property-renderers) is enabled (not null), the method will use it to render the view file.
Otherwise, it will simply include the view file as a normal PHP file, capture its output and
return it as a string.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$viewFile` | `string` | The view file. This can be either an absolute file path or an alias of it. |
| `$params` | `array` | The parameters (name-value pairs) that will be extracted and made available in the view file. |
| `$context` | `object\|null` | The context that the view should use for rendering the view. If null, existing [`$context`](/docs/api/yii/base/view/index.md#property-context) will be used. |

**Returns**

`string`: The rendering result

**Throws**

- [`ViewNotFoundException`](/docs/api/yii/base/viewnotfoundexception/index.md): if the view file does not exist

### renderPhpFile() <span id="method-renderPhpFile"></span>

```php
public function renderPhpFile(string $_file_, array $_params_ = []): string
```

Renders a view file as a PHP script.

This method treats the view file as a PHP script and includes the file.
It extracts the given parameters and makes them available in the view file.
The method captures the output of the included view file and returns it as a string.

This method should mainly be called by view renderer or [`renderFile()`](/docs/api/yii/base/view/index.md#method-renderFile).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$_file_` | `string` | The view file. |
| `$_params_` | `array` | The parameters (name-value pairs) that will be extracted and made available in the view file. |

**Returns**

`string`: The rendering result

**Throws**

- [`Throwable`](https://www.php.net/manual/en/class.throwable.php)

### setDynamicPlaceholders() <span id="method-setDynamicPlaceholders"></span>

```php
public function setDynamicPlaceholders(mixed $placeholders): mixed
```

Sets a list of placeholders for dynamic content. This method
is used internally to implement the content caching feature.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$placeholders` | `mixed` | A list of placeholders. |

**Returns**

`mixed`

### findViewFile() <span id="method-findViewFile"></span>

```php
protected function findViewFile(string $view, object|null $context = null): string
```

Finds the view file based on the given view name.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$view` | `string` | The view name or the [path alias](/docs/guide/concept-aliases/index.md) of the view file. Please refer to [`render()`](/docs/api/yii/base/view/index.md#method-render) on how to specify this parameter. |
| `$context` | `object\|null` | The context to be assigned to the view and can later be accessed via [`$context`](/docs/api/yii/base/view/index.md#property-context) in the view. If the context implements [`ViewContextInterface`](/docs/api/yii/base/viewcontextinterface/index.md), it may also be used to locate the view file corresponding to a relative view name. |

**Returns**

`string`: The view file path. Note that the file may not exist.

**Throws**

- [`InvalidCallException`](/docs/api/yii/base/invalidcallexception/index.md): if a relative view name is given while there is no active context to determine the corresponding view file.

### getRequestedViewFile() <span id="method-getRequestedViewFile"></span>

```php
protected function getRequestedViewFile(): string|bool
```

**Returns**

`string|bool`: The requested view currently being rendered. False if no view file is being rendered.

**Since**

2.0.16