# Class yii\web\View

Inheritance | yii\web\View » yii\base\View » yii\base\Component » yii\base\BaseObject
Implements | yii\base\Configurable, yii\base\DynamicContentAwareInterface
Available since version | 2.0
Source Code | https://github.com/yiisoft/yii2/blob/master/framework/web/View.php

View represents a view object in the MVC pattern.

View provides a set of methods (e.g. render()) for rendering purpose.

View is configured as an application component in yii\base\Application by default. You can access that instance via `Yii::$app->view`.

You can modify its configuration by adding an array to your application config under `components` as it is shown in the following example:

    'view' => [
        'theme' => 'app\themes\MyTheme',
        'renderers' => [
            // you may add Smarty or Twig renderer here
        ]
        // ...
    ]

For more details and usage information on View, see the guide article on views.

## Public Properties

Hide inherited properties

Property | Type | Description | Defined By
--- | --- | --- | ---
$assetBundles | yii\web\AssetBundle[] | List of the registered asset bundles. | yii\web\View
$assetManager | yii\web\AssetManager | The asset manager. Defaults to the "assetManager" application component. | yii\web\View
$behaviors | yii\base\Behavior[] | List of behaviors attached to this component. This property is read-only. | yii\base\Component
$blocks | array | A list of named output blocks. | yii\base\View
$cacheStack | array|yii\base\DynamicContentAwareInterface[] | A list of currently active dynamic content class instances. | yii\base\View
$context | yii\base\ViewContextInterface | The context under which the renderFile() method is being invoked. | yii\base\View
$css | array | The registered CSS code blocks. | yii\web\View
$cssFiles | array | The registered CSS files. | yii\web\View
$defaultExtension | string | The default view file extension. | yii\base\View
$dynamicContents | yii\base\DynamicContentAwareInterface[] | Class instances supporting dynamic contents. | yii\base\View
$dynamicPlaceholders | array | A list of placeholders for embedding dynamic contents. | yii\base\View
$js | array | The registered JS code blocks | yii\web\View
$jsFiles | array | The registered JS files. | yii\web\View
$linkTags | array | The registered link tags. | yii\web\View
$metaTags | array | The registered meta tags. | yii\web\View
$params | array | Custom parameters that are shared among view templates. | yii\base\View
$renderers | array | A list of available renderers indexed by their corresponding supported file extensions. | yii\base\View
$theme | yii\base\Theme|array|string | The theme object or the configuration for creating the theme object. | yii\base\View
$title | string | The page title | yii\web\View
$viewFile | string|boolean | The view file currently being rendered. False if no view file is being rendered. This property is read-only. | yii\base\View

## Public Methods

Hide inherited methods

Method | Description | Defined By
--- | --- | ---
__call() | Calls the named method which is not a class method. | yii\base\Component
__clone() | This method is called after the object is created by cloning an existing one. | yii\base\Component
__construct() | Constructor. | yii\base\BaseObject
__get() | Returns the value of a component property. | yii\base\Component
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\Component
__set() | Sets the value of a component property. | yii\base\Component
__unset() | Sets a component property to be null. | yii\base\Component
addDynamicPlaceholder() | Adds a placeholder for dynamic content. | yii\base\View
afterRender() | This method is invoked right after renderFile() renders a view file. | yii\base\View
attachBehavior() | Attaches a behavior to this component. | yii\base\Component
attachBehaviors() | Attaches a list of behaviors to the component. | yii\base\Component
beforeRender() | This method is invoked right before renderFile() renders a view file. | yii\base\View
beginBlock() | Begins recording a block. | yii\base\View
beginBody() | Marks the beginning of an HTML body section. | yii\web\View
beginCache() | Begins fragment caching. | yii\base\View
beginContent() | Begins the rendering of content that is to be decorated by the specified view. | yii\base\View
beginPage() | Marks the beginning of a page. | yii\base\View
behaviors() | Returns a list of behaviors that this component should behave as. | yii\base\Component
canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\Component
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\Component
className() | Returns the fully qualified name of this class. | yii\base\BaseObject
clear() | Clears up the registered meta tags, link tags, css/js scripts and files. | yii\web\View
detachBehavior() | Detaches a behavior from the component. | yii\base\Component
detachBehaviors() | Detaches all behaviors from the component. | yii\base\Component
endBlock() | Ends recording a block. | yii\base\View
endBody() | Marks the ending of an HTML body section. | yii\web\View
endCache() | Ends fragment caching. | yii\base\View
endContent() | Ends the rendering of content. | yii\base\View
endPage() | Marks the ending of an HTML page. | yii\web\View
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\base\Component
evaluateDynamicContent() | Evaluates the given PHP statements. | yii\base\View
getAssetManager() | Registers the asset manager being used by this view object. | yii\web\View
getBehavior() | Returns the named behavior object. | yii\base\Component
getBehaviors() | Returns all behaviors attached to this component. | yii\base\Component
getDynamicContents() | Returns a list of currently active dynamic content class instances. | yii\base\View
getDynamicPlaceholders() | Returns a list of placeholders for dynamic content. This method is used internally to implement the content caching feature. | yii\base\View
getViewFile() |  | yii\base\View
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\base\Component
hasMethod() | Returns a value indicating whether a method is defined. | yii\base\Component
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\base\Component
head() | Marks the position of an HTML head section. | yii\web\View
init() | Initializes the view component. | yii\base\View
off() | Detaches an existing event handler from this component. | yii\base\Component
on() | Attaches an event handler to an event. | yii\base\Component
popDynamicContent() | Removes a last class instance supporting dynamic contents from a list of currently active dynamic content class instances. | yii\base\View
pushDynamicContent() | Adds a class instance supporting dynamic contents to the end of a list of currently active dynamic content class instances. | yii\base\View
registerAssetBundle() | Registers the named asset bundle. | yii\web\View
registerCsrfMetaTags() | Registers CSRF meta tags. | yii\web\View
registerCss() | Registers a CSS code block. | yii\web\View
registerCssFile() | Registers a CSS file. | yii\web\View
registerJs() | Registers a JS code block. | yii\web\View
registerJsFile() | Registers a JS file. | yii\web\View
registerJsVar() | Registers a JS code block defining a variable. The name of variable will be used as key, preventing duplicated variable names. | yii\web\View
registerLinkTag() | Registers a link tag. | yii\web\View
registerMetaTag() | Registers a meta tag. | yii\web\View
render() | Renders a view. | yii\base\View
renderAjax() | Renders a view in response to an AJAX request. | yii\web\View
renderDynamic() | Renders dynamic content returned by the given PHP statements. | yii\base\View
renderFile() | Renders a view file. | yii\base\View
renderPhpFile() | Renders a view file as a PHP script. | yii\base\View
setAssetManager() | Sets the asset manager. | yii\web\View
setDynamicPlaceholders() | Sets a list of placeholders for dynamic content. This method is used internally to implement the content caching feature. | yii\base\View
trigger() | Triggers an event. | yii\base\Component

## Protected Methods

Hide inherited methods

Method | Description | Defined By
--- | --- | ---
findViewFile() | Finds the view file based on the given view name. | yii\base\View
getRequestedViewFile() |  | yii\base\View
registerAssetFiles() | Registers all files provided by an asset bundle including depending bundles files. | yii\web\View
renderBodyBeginHtml() | Renders the content to be inserted at the beginning of the body section. | yii\web\View
renderBodyEndHtml() | Renders the content to be inserted at the end of the body section. | yii\web\View
renderHeadHtml() | Renders the content to be inserted in the head section. | yii\web\View

## Events

Hide inherited events

Event | Type | Description | Defined By
--- | --- | --- | ---
EVENT_AFTER_RENDER | yii\base\ViewEvent | An event that is triggered by renderFile() right after it renders a view file. | yii\base\View
EVENT_BEFORE_RENDER | yii\base\ViewEvent | An event that is triggered by renderFile() right before it renders a view file. | yii\base\View
EVENT_BEGIN_BODY | \yii\web\Event | An event that is triggered by beginBody(). | yii\web\View
EVENT_BEGIN_PAGE | yii\base\Event | An event that is triggered by beginPage(). | yii\base\View
EVENT_END_BODY | \yii\web\Event | An event that is triggered by endBody(). | yii\web\View
EVENT_END_PAGE | yii\base\Event | An event that is triggered by endPage(). | yii\base\View

## Constants

Hide inherited constants

Constant | Value | Description | Defined By
--- | --- | --- | ---
PH_BODY_BEGIN | '' | This is internally used as the placeholder for receiving the content registered for the beginning of the body section. | yii\web\View
PH_BODY_END | ' ' | This is internally used as the placeholder for receiving the content registered for the end of the body section. | yii\web\View
PH_HEAD | ' ' | This is internally used as the placeholder for receiving the content registered for the head section. | yii\web\View
POS_BEGIN | 2 | The location of registered JavaScript code block or files. This means the location is at the beginning of the body section. | yii\web\View
POS_END | 3 | The location of registered JavaScript code block or files. This means the location is at the end of the body section. | yii\web\View
POS_HEAD | 1 | The location of registered JavaScript code block or files. This means the location is in the head section. | yii\web\View
POS_LOAD | 5 | The location of registered JavaScript code block. This means the JavaScript code block will be enclosed within `jQuery(window).load()`. | yii\web\View
POS_READY | 4 | The location of registered JavaScript code block. This means the JavaScript code block will be enclosed within `jQuery(document).ready()`. | yii\web\View

## Property Details

$assetBundles public property

List of the registered asset bundles. The keys are the bundle names, and the values are the registered yii\web\AssetBundle objects.

See also registerAssetBundle().

public yii\web\AssetBundle[] $assetBundles = []
$assetManager public property

The asset manager. Defaults to the "assetManager" application component.

public yii\web\AssetManager $assetManager = null
$css public property

The registered CSS code blocks.

See also registerCss().

public array $css = []
$cssFiles public property

The registered CSS files.

See also registerCssFile().

public array $cssFiles = []
$js public property

The registered JS code blocks

See also registerJs().

public array $js = []
$jsFiles public property

The registered JS files.

See also registerJsFile().

public array $jsFiles = []
$linkTags public property

The registered link tags.

See also registerLinkTag().

public array $linkTags = []
$metaTags public property

The registered meta tags.

See also registerMetaTag().

public array $metaTags = []
$title public property

The page title

public string $title = null

## Method Details

beginBody() public method

Marks the beginning of an HTML body section.

public void beginBody ( )

clear() public method

Clears up the registered meta tags, link tags, css/js scripts and files.

public void clear ( )

endBody() public method

Marks the ending of an HTML body section.

public void endBody ( )

endPage() public method

Marks the ending of an HTML page.

public void endPage ( $ajaxMode = false )
$ajaxMode | boolean | Whether the view is rendering in AJAX mode. If true, the JS scripts registered at POS_READY and POS_LOAD positions will be rendered at the end of the view like normal scripts.

getAssetManager() public method

Registers the asset manager being used by this view object.

public yii\web\AssetManager getAssetManager ( )
return | yii\web\AssetManager | The asset manager. Defaults to the "assetManager" application component.

head() public method

Marks the position of an HTML head section.

public void head ( )

registerAssetBundle() public method

Registers the named asset bundle.

All dependent asset bundles will be registered.

public yii\web\AssetBundle registerAssetBundle ( $name, $position = null )
$name | string | The class name of the asset bundle (without the leading backslash)
$position | integer|null | If set, this forces a minimum position for javascript files. This will adjust depending assets javascript file position or fail if requirement can not be met. If this is null, asset bundles position settings will not be changed. See registerJsFile() for more details on javascript position.
return | yii\web\AssetBundle | The registered asset bundle instance
throws | yii\base\InvalidConfigException | if the asset bundle does not exist or a circular dependency is detected

registerAssetFiles() protected method

Registers all files provided by an asset bundle including depending bundles files.

Removes a bundle from $assetBundles once files are registered.

protected void registerAssetFiles ( $name )
$name | string | Name of the bundle to register

registerCsrfMetaTags() public method (available since version 2.0.13)

Registers CSRF meta tags.

They are rendered dynamically to retrieve a new CSRF token for each request.

    $view->registerCsrfMetaTags();

The above code will result in `<meta name="csrf-param" content="[yii\web\Request::$csrfParam]">` and `<meta name="csrf-token" content="tTNpWKpdy-bx8ZmIq9R72...K1y8IP3XGkzZA==">` added to the page.

Note: Hidden CSRF input of ActiveForm will be automatically refreshed by calling `window.yii.refreshCsrfToken()` from `yii.js`.

public void registerCsrfMetaTags ( )

registerCss() public method

Registers a CSS code block.

public void registerCss ( $css, $options = [], $key = null )
$css | string | The content of the CSS code block to be registered
$options | array | The HTML attributes for the `<style>`-tag.
$key | string | The key that identifies the CSS code block. If null, it will use $css as the key. If two CSS code blocks are registered with the same key, the latter will overwrite the former.

registerCssFile() public method

Registers a CSS file.

This method should be used for simple registration of CSS files. If you want to use features of yii\web\AssetManager like appending timestamps to the URL and file publishing options, use yii\web\AssetBundle and registerAssetBundle() instead.

public void registerCssFile ( $url, $options = [], $key = null )
$url | string | The CSS file to be registered.

$options

array

The HTML attributes for the link tag. Please refer to yii\helpers\Html::cssFile() for the supported options. The following options are specially handled and are not treated as HTML attributes:

- `depends`: array, specifies the names of the asset bundles that this CSS file depends on.
- `appendTimestamp`: bool whether to append a timestamp to the URL.

$key | string | The key that identifies the CSS script file. If null, it will use $url as the key. If two CSS files are registered with the same key, the latter will overwrite the former.
throws | yii\base\InvalidConfigException |

registerJs() public method

Registers a JS code block.

public void registerJs ( $js, $position = self::POS_READY, $key = null )
$js | string | The JS code block to be registered

$position

integer

The position at which the JS script tag should be inserted in a page. The possible values are:

- POS_HEAD: in the head section
- POS_BEGIN: at the beginning of the body section
- POS_END: at the end of the body section
- POS_LOAD: enclosed within jQuery(window).load(). Note that by using this position, the method will automatically register the jQuery js file.
- POS_READY: enclosed within jQuery(document).ready(). This is the default value. Note that by using this position, the method will automatically register the jQuery js file.

$key | string | The key that identifies the JS code block. If null, it will use $js as the key. If two JS code blocks are registered with the same key, the latter will overwrite the former.

registerJsFile() public method

Registers a JS file.

This method should be used for simple registration of JS files. If you want to use features of yii\web\AssetManager like appending timestamps to the URL and file publishing options, use yii\web\AssetBundle and registerAssetBundle() instead.

public void registerJsFile ( $url, $options = [], $key = null )
$url | string | The JS file to be registered.

$options

array

The HTML attributes for the script tag. The following options are specially handled and are not treated as HTML attributes:

- `depends`: array, specifies the names of the asset bundles that this JS file depends on.
- `position`: specifies where the JS script tag should be inserted in a page. The possible values are:
  - POS_HEAD: in the head section
  - POS_BEGIN: at the beginning of the body section
  - POS_END: at the end of the body section. This is the default value.
- `appendTimestamp`: bool whether to append a timestamp to the URL.

Please refer to yii\helpers\Html::jsFile() for other supported options.

$key | string | The key that identifies the JS script file. If null, it will use $url as the key. If two JS files are registered with the same key at the same position, the latter will overwrite the former. Note that position option takes precedence, thus files registered with the same key, but different position option will not override each other.
throws | yii\base\InvalidConfigException |

registerJsVar() public method (available since version 2.0.14)

Registers a JS code block defining a variable. The name of variable will be used as key, preventing duplicated variable names.

public void registerJsVar ( $name, $value, $position = self::POS_HEAD )
$name | string | Name of the variable
$value | array|string | Value of the variable

$position

integer

The position in a page at which the JavaScript variable should be inserted. The possible values are:

- POS_HEAD: in the head section. This is the default value.
- POS_BEGIN: at the beginning of the body section.
- POS_END: at the end of the body section.
- POS_LOAD: enclosed within jQuery(window).load(). Note that by using this position, the method will automatically register the jQuery js file.
- POS_READY: enclosed within jQuery(document).ready(). Note that by using this position, the method will automatically register the jQuery js file.

registerLinkTag() public method

Registers a link tag.

For example, a link tag for a custom favicon can be added like the following:

    $view->registerLinkTag(['rel' => 'icon', 'type' => 'image/png', 'href' => '/myicon.png']);

which will result in the following HTML: `<link rel="icon" type="image/png" href="/myicon.png">`.

Note: To register link tags for CSS stylesheets, use registerCssFile() instead, which has more options for this kind of link tag.

public void registerLinkTag ( $options, $key = null )
$options | array | The HTML attributes for the link tag.
$key | string | The key that identifies the link tag. If two link tags are registered with the same key, the latter will overwrite the former. If this is null, the new link tag will be appended to the existing ones.

registerMetaTag() public method

Registers a meta tag.

For example, a description meta tag can be added like the following:

    $view->registerMetaTag([
        'name' => 'description',
        'content' => 'This website is about funny raccoons.'
    ]);

will result in the meta tag `<meta name="description" content="This website is about funny raccoons.">`.

public void registerMetaTag ( $options, $key = null )
$options | array | The HTML attributes for the meta tag.
$key | string | The key that identifies the meta tag. If two meta tags are registered with the same key, the latter will overwrite the former. If this is null, the new meta tag will be appended to the existing ones.

renderAjax() public method

Renders a view in response to an AJAX request.

This method is similar to render() except that it will surround the view being rendered with the calls of beginPage(), head(), beginBody(), endBody() and endPage(). By doing so, the method is able to inject into the rendering result with JS/CSS scripts and files that are registered with the view.

See also render().

public string renderAjax ( $view, $params = [], $context = null )
$view | string | The view name. Please refer to render() on how to specify this parameter.
$params | array | The parameters (name-value pairs) that will be extracted and made available in the view file.
$context | object | The context that the view should use for rendering the view. If null, existing $context will be used.
return | string | The rendering result

renderBodyBeginHtml() protected method

Renders the content to be inserted at the beginning of the body section.

The content is rendered using the registered JS code blocks and files.

protected string renderBodyBeginHtml ( )
return | string | The rendered content

renderBodyEndHtml() protected method

Renders the content to be inserted at the end of the body section.

The content is rendered using the registered JS code blocks and files.

protected string renderBodyEndHtml ( $ajaxMode )
$ajaxMode | boolean | Whether the view is rendering in AJAX mode. If true, the JS scripts registered at POS_READY and POS_LOAD positions will be rendered at the end of the view like normal scripts.
return | string | The rendered content

renderHeadHtml() protected method

Renders the content to be inserted in the head section.

The content is rendered using the registered meta tags, link tags, CSS/JS code blocks and files.

protected string renderHeadHtml ( )
return | string | The rendered content

setAssetManager() public method

Sets the asset manager.

public void setAssetManager ( $value )
$value | yii\web\AssetManager | The asset manager

## Event Details

EVENT_BEGIN_BODY event of type \yii\web\Event

An event that is triggered by beginBody().

EVENT_END_BODY event of type \yii\web\Event

An event that is triggered by endBody().