View
API reference › yii › web › View
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 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.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$assetBundles | array<string, AssetBundle| | List of the registered asset bundles. Default: []. | View |
$assetManager | AssetManager | The asset manager. | View |
$behaviors read-only | array<yii\<static>> | List of behaviors attached to this component. | yii\ |
$blocks | array | A list of named output blocks. Default: null. | yii\ |
$context | yii\ | The context under which the renderFile() method is being invoked. Default: null. | yii\ |
$css | array | The registered CSS code blocks. Default: []. | View |
$cssFiles | array | The registered CSS files. Default: []. | View |
$defaultExtension | string | The default view file extension. Default: 'php'. | yii\ |
$dynamicContents read-only | yii\[] | Class instances supporting dynamic contents. | yii\ |
$dynamicPlaceholders | array | A list of placeholders. | yii\ |
$js | array | The registered JS code blocks Default: []. | View |
$jsFiles | array | The registered JS files. Default: []. | View |
$linkTags | array | The registered link tags. Default: []. | View |
$metaTags | array | The registered meta tags. Default: []. | View |
$params | array | Custom parameters that are shared among view templates. Default: []. | yii\ |
$renderers | array| | A list of available renderers indexed by their corresponding supported file extensions. Default: null. | yii\ |
$scriptOptions | array | The script tag options. Default: []. Available since 2.0.50. | View |
$styleOptions | array | The style tag options. Default: []. Available since 2.0.53. | View |
$theme | yii\| | The theme object or the configuration for creating the theme object. Default: null. | yii\ |
$title | string | The page title Default: null. | View |
$viewFile read-only | string| | The view file currently being rendered. | yii\ |
$isPageEnded protected | bool | Whether endPage() has been called and all files have been registered Default: false. Available since 2.0.44. | View |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__clone() | This method is called after the object is created by cloning an existing one. | yii\ |
__get() | Returns the value of a component property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets the value of a component property. | yii\ |
__unset() | Sets a component property to be null. | yii\ |
addDynamicPlaceholder() | Adds a placeholder for dynamic content. | yii\ |
afterRender() | This method is invoked right after renderFile() renders a view file. | yii\ |
attachBehavior() | Attaches a behavior to this component. | yii\ |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\ |
beforeRender() | This method is invoked right before renderFile() renders a view file. | yii\ |
beginBlock() | Begins recording a block. | yii\ |
beginBody() | Marks the beginning of an HTML body section. | View |
beginCache() | Begins fragment caching. | yii\ |
beginContent() | Begins the rendering of content that is to be decorated by the specified view. | yii\ |
beginPage() | Marks the beginning of a page. | yii\ |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
clear() | Clears up the registered meta tags, link tags, css/js scripts and files. | View |
detachBehavior() | Detaches a behavior from the component. | yii\ |
detachBehaviors() | Detaches all behaviors from the component. | yii\ |
endBlock() | Ends recording a block. | yii\ |
endBody() | Marks the ending of an HTML body section. | View |
endCache() | Ends fragment caching. | yii\ |
endContent() | Ends the rendering of content. | yii\ |
endPage() | Marks the ending of an HTML page. | View |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\ |
evaluateDynamicContent() | Evaluates the given PHP statements. | yii\ |
getAssetManager() | Registers the asset manager being used by this view object. | View |
getBehavior() | Returns the named behavior object. | yii\ |
getBehaviors() | Returns all behaviors attached to this component. | yii\ |
getDynamicContents() | Returns a list of currently active dynamic content class instances. | yii\ |
getDynamicPlaceholders() | Returns a list of placeholders for dynamic content. This method is used internally to implement the content caching feature. | yii\ |
getViewFile() | yii\ | |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\ |
head() | Marks the position of an HTML head section. | View |
init() | Initializes the view component. | yii\ |
off() | Detaches an existing event handler from this component. | yii\ |
on() | Attaches an event handler to an event. | yii\ |
popDynamicContent() | Removes a last class instance supporting dynamic contents from a list of currently active dynamic content class instances. | yii\ |
pushDynamicContent() | Adds a class instance supporting dynamic contents to the end of a list of currently active dynamic content class instances. | yii\ |
registerAssetBundle() | Registers the named asset bundle. | View |
registerCsrfMetaTags() | Registers CSRF meta tags. | View |
registerCss() | Registers a CSS code block. | View |
registerCssFile() | Registers a CSS file. | View |
registerJs() | Registers a JS code block. | View |
registerJsFile() | Registers a JS file. | View |
registerJsVar() | Registers a JS code block defining a variable. The name of variable will be used as key, preventing duplicated variable names. | View |
registerLinkTag() | Registers a link tag. | View |
registerMetaTag() | Registers a meta tag. | View |
render() | Renders a view. | yii\ |
renderAjax() | Renders a view in response to an AJAX request. | View |
renderDynamic() | Renders dynamic content returned by the given PHP statements. | yii\ |
renderFile() | Renders a view file. | yii\ |
renderPhpFile() | Renders a view file as a PHP script. | yii\ |
setAssetManager() | Sets the asset manager. | View |
setDynamicPlaceholders() | Sets a list of placeholders for dynamic content. This method is used internally to implement the content caching feature. | yii\ |
trigger() | Triggers an event. | yii\ |
findViewFile() protected | Finds the view file based on the given view name. | yii\ |
getRequestedViewFile() protected | yii\ | |
registerAssetFiles() protected | Registers all files provided by an asset bundle including depending bundles files. | View |
renderBodyBeginHtml() protected | Renders the content to be inserted at the beginning of the body section. | View |
renderBodyEndHtml() protected | Renders the content to be inserted at the end of the body section. | View |
renderHeadHtml() protected | Renders the content to be inserted in the head section. | View |
wrapLoadScript() protected | Wraps script content for POS_LOAD position. | View |
wrapReadyScript() protected | Wraps script content for POS_READY position. | View |
Events
Section titled “Events”| Event | Type | Description | Defined by |
|---|---|---|---|
EVENT_BEGIN_BODY | yii\ | An event that is triggered by beginBody(). Event name: 'beginBody'. | View |
EVENT_END_BODY | yii\ | An event that is triggered by endBody(). Event name: 'endBody'. | View |
EVENT_BEGIN_PAGE | yii\ | An event that is triggered by beginPage(). Event name: 'beginPage'. | yii\ |
EVENT_END_PAGE | yii\ | An event that is triggered by endPage(). Event name: 'endPage'. | yii\ |
EVENT_BEFORE_RENDER | yii\ | An event that is triggered by renderFile() right before it renders a view file. Event name: 'beforeRender'. | yii\ |
EVENT_AFTER_RENDER | yii\ | An event that is triggered by renderFile() right after it renders a view file. Event name: 'afterRender'. | yii\ |
Constants
Section titled “Constants”| Constant | Value | Description | Defined by |
|---|---|---|---|
POS_HEAD | 1 | The location of registered JavaScript code block or files. | View |
POS_BEGIN | 2 | The location of registered JavaScript code block or files. | View |
POS_END | 3 | The location of registered JavaScript code block or files. | View |
POS_READY | 4 | The location of registered JavaScript code block. | View |
POS_LOAD | 5 | The location of registered JavaScript code block. | View |
PH_HEAD | '<![CDATA[YII-BLOCK-HEAD]]>' | This is internally used as the placeholder for receiving the content registered for the head section. | View |
PH_BODY_BEGIN | '<![CDATA[YII-BLOCK-BODY-BEGIN]]>' | This is internally used as the placeholder for receiving the content registered for the beginning of the body section. | View |
PH_BODY_END | '<![CDATA[YII-BLOCK-BODY-END]]>' | This is internally used as the placeholder for receiving the content registered for the end of the body section. | View |
Property details
Section titled “Property details”$assetBundles
Section titled “$assetBundles ”public array<string, yii\web\AssetBundle|bool> $assetBundles = []List of the registered asset bundles.
The keys are the
bundle names, and the values are the registered AssetBundle objects.
$assetManager
Section titled “$assetManager ”public yii\web\AssetManager $assetManagerThe asset manager.
Defaults to the “assetManager” application component.
Method details
Section titled “Method details”beginBody()
Section titled “beginBody() ”public function beginBody(): mixedMarks the beginning of an HTML body section.
Returns
mixed
clear()
Section titled “clear() ”public function clear(): mixedClears up the registered meta tags, link tags, css/js scripts and files.
Returns
mixed
endBody()
Section titled “endBody() ”public function endBody(): mixedMarks the ending of an HTML body section.
Returns
mixed
endPage()
Section titled “endPage() ”public function endPage(bool $ajaxMode = false): mixedMarks the ending of an HTML page.
getAssetManager()
Section titled “getAssetManager() ”public function getAssetManager(): yii\web\AssetManagerRegisters the asset manager being used by this view object.
Returns
AssetManager: The asset manager. Defaults to the “assetManager” application component.
head()
Section titled “head() ”public function head(): mixedMarks the position of an HTML head section.
Returns
mixed
registerAssetBundle()
Section titled “registerAssetBundle() ”public function registerAssetBundle(string $name, int|null $position = null): yii\web\AssetBundleRegisters the named asset bundle.
All dependent asset bundles will be registered.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The class name of the asset bundle (without the leading backslash) |
$position | int| | 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. |
Returns
AssetBundle: The registered asset bundle instance
Throws
yii\base\InvalidConfigException: if the asset bundle does not exist or a circular dependency is detected
registerCsrfMetaTags()
Section titled “registerCsrfMetaTags() ”public function registerCsrfMetaTags(): mixedRegisters 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.
Returns
mixed
Since
2.0.13
registerCss()
Section titled “registerCss() ”public function registerCss(string $css, array $options = [], string|null $key = null): mixedRegisters a CSS code block.
Parameters
| Parameter | Type | Description |
|---|---|---|
$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. |
Returns
mixed
registerCssFile()
Section titled “registerCssFile() ”public function registerCssFile(string $url, yii\web\RegisterCssFileOptions $options = [], string|null $key = null): mixedRegisters a CSS file.
This method should be used for simple registration of CSS files. If you want to use features of
AssetManager like appending timestamps to the URL and file publishing options, use AssetBundle
and registerAssetBundle() instead.
Parameters
| Parameter | Type | Description |
|---|---|---|
$url | string | The CSS file to be registered. |
$options | yii\ | The HTML attributes for the link tag. Please refer to 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. |
Returns
mixed
Throws
registerJs()
Section titled “registerJs() ”public function registerJs(string $js, int $position = \self::POS_READY, string|null $key = null): mixedRegisters a JS code block.
Parameters
| Parameter | Type | Description |
|---|---|---|
$js | string | The JS code block to be registered |
$position | int | 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 a window load event listener. - POS_READY: enclosed within a DOMContentLoaded event listener. This is the default value. |
$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. |
Returns
mixed
registerJsFile()
Section titled “registerJsFile() ”public function registerJsFile(string $url, yii\web\RegisterJsFileOptions $options = [], string|null $key = null): mixedRegisters a JS file.
This method should be used for simple registration of JS files. If you want to use features of
AssetManager like appending timestamps to the URL and file publishing options, use AssetBundle
and registerAssetBundle() instead.
Parameters
| Parameter | Type | Description |
|---|---|---|
$url | string | The JS file to be registered. |
$options | yii\ | 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 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. |
Returns
mixed
Throws
registerJsVar()
Section titled “registerJsVar() ”public function registerJsVar(string $name, array|string $value, int $position = \self::POS_HEAD): mixedRegisters a JS code block defining a variable. The name of variable will be used as key, preventing duplicated variable names.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | Name of the variable |
$value | array| | Value of the variable |
$position | int | 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 a window load event listener. - POS_READY: enclosed within a DOMContentLoaded event listener. |
Returns
mixed
Since
2.0.14
registerLinkTag()
Section titled “registerLinkTag() ”public function registerLinkTag(array $options, string|null $key = null): mixedRegisters 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.
Parameters
| Parameter | Type | Description |
|---|---|---|
$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. |
Returns
mixed
registerMetaTag()
Section titled “registerMetaTag() ”public function registerMetaTag(array $options, string|null $key = null): mixedRegisters 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.">.
Parameters
| Parameter | Type | Description |
|---|---|---|
$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. |
Returns
mixed
renderAjax()
Section titled “renderAjax() ”public function renderAjax(string $view, array $params = [], object|null $context = null): stringRenders 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.
Parameters
| Parameter | Type | Description |
|---|---|---|
$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. |
Returns
string: The rendering result
See also
setAssetManager()
Section titled “setAssetManager() ”public function setAssetManager(yii\web\AssetManager $value): mixedSets the asset manager.
registerAssetFiles()
Section titled “registerAssetFiles() ”protected function registerAssetFiles(string $name): mixedRegisters all files provided by an asset bundle including depending bundles files.
Removes a bundle from $assetBundles once files are registered.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | Name of the bundle to register |
Returns
mixed
renderBodyBeginHtml()
Section titled “renderBodyBeginHtml() ”protected function renderBodyBeginHtml(): stringRenders the content to be inserted at the beginning of the body section.
The content is rendered using the registered JS code blocks and files.
Returns
string: The rendered content
renderBodyEndHtml()
Section titled “renderBodyEndHtml() ”protected function renderBodyEndHtml(bool $ajaxMode): stringRenders the content to be inserted at the end of the body section.
The content is rendered using the registered JS code blocks and files.
renderHeadHtml()
Section titled “renderHeadHtml() ”protected function renderHeadHtml(): stringRenders 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.
Returns
string: The rendered content
wrapLoadScript()
Section titled “wrapLoadScript() ”protected function wrapLoadScript(string $script): stringWraps script content for POS_LOAD position.
Parameters
| Parameter | Type | Description |
|---|---|---|
$script | string | The script content to wrap |
Returns
string: The wrapped script
Since
22.0
wrapReadyScript()
Section titled “wrapReadyScript() ”protected function wrapReadyScript(string $script): stringWraps script content for POS_READY position.
Parameters
| Parameter | Type | Description |
|---|---|---|
$script | string | The script content to wrap |
Returns
string: The wrapped script
Since
22.0