View
API reference › yii › base › View
1 subclass
View represents a view object in the MVC pattern.
View provides a set of methods (e.g. render()) for rendering purpose.
For more details and usage information on View, see the guide article on views.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$behaviors read-only | array<Behavior<static>> | List of behaviors attached to this component. | Component |
$blocks | array | A list of named output blocks. Default: null. | View |
$context | ViewContextInterface | The context under which the renderFile() method is being invoked. Default: null. | View |
$defaultExtension | string | The default view file extension. Default: 'php'. | View |
$dynamicContents read-only | DynamicContentAwareInterface[] | Class instances supporting dynamic contents. | View |
$dynamicPlaceholders | array | A list of placeholders. | View |
$params | array | Custom parameters that are shared among view templates. Default: []. | View |
$renderers | array| | A list of available renderers indexed by their corresponding supported file extensions. Default: null. | View |
$theme | Theme| | The theme object or the configuration for creating the theme object. Default: null. | View |
$viewFile read-only | string| | The view file currently being rendered. | View |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | BaseObject |
__call() | Calls the named method which is not a class method. | Component |
__clone() | This method is called after the object is created by cloning an existing one. | Component |
__get() | Returns the value of a component property. | Component |
__isset() | Checks if a property is set, i.e. defined and not null. | Component |
__set() | Sets the value of a component property. | Component |
__unset() | Sets a component property to be null. | Component |
addDynamicPlaceholder() | Adds a placeholder for dynamic content. | View |
afterRender() | This method is invoked right after renderFile() renders a view file. | View |
attachBehavior() | Attaches a behavior to this component. | Component |
attachBehaviors() | Attaches a list of behaviors to the component. | Component |
beforeRender() | This method is invoked right before renderFile() renders a view file. | View |
beginBlock() | Begins recording a block. | View |
beginCache() | Begins fragment caching. | View |
beginContent() | Begins the rendering of content that is to be decorated by the specified view. | View |
beginPage() | Marks the beginning of a page. | View |
behaviors() | Returns a list of behaviors that this component should behave as. | Component |
canGetProperty() | Returns a value indicating whether a property can be read. | Component |
canSetProperty() | Returns a value indicating whether a property can be set. | Component |
className() static | Returns the fully qualified name of this class. | BaseObject |
detachBehavior() | Detaches a behavior from the component. | Component |
detachBehaviors() | Detaches all behaviors from the component. | Component |
endBlock() | Ends recording a block. | View |
endCache() | Ends fragment caching. | View |
endContent() | Ends the rendering of content. | View |
endPage() | Marks the ending of a page. | View |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | Component |
evaluateDynamicContent() | Evaluates the given PHP statements. | View |
getBehavior() | Returns the named behavior object. | Component |
getBehaviors() | Returns all behaviors attached to this component. | Component |
getDynamicContents() | Returns a list of currently active dynamic content class instances. | View |
getDynamicPlaceholders() | Returns a list of placeholders for dynamic content. This method is used internally to implement the content caching feature. | View |
getViewFile() | View | |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | Component |
hasMethod() | Returns a value indicating whether a method is defined. | Component |
hasProperty() | Returns a value indicating whether a property is defined for this component. | Component |
init() | Initializes the view component. | View |
off() | Detaches an existing event handler from this component. | Component |
on() | Attaches an event handler to an event. | Component |
popDynamicContent() | Removes a last class instance supporting dynamic contents from a list of currently active dynamic content class instances. | View |
pushDynamicContent() | Adds a class instance supporting dynamic contents to the end of a list of currently active dynamic content class instances. | View |
render() | Renders a view. | View |
renderDynamic() | Renders dynamic content returned by the given PHP statements. | View |
renderFile() | Renders a view file. | View |
renderPhpFile() | Renders a view file as a PHP script. | View |
setDynamicPlaceholders() | Sets a list of placeholders for dynamic content. This method is used internally to implement the content caching feature. | View |
trigger() | Triggers an event. | Component |
findViewFile() protected | Finds the view file based on the given view name. | View |
getRequestedViewFile() protected | View |
Events
Section titled “Events”| Event | Type | Description | Defined by |
|---|---|---|---|
EVENT_BEGIN_PAGE | Event | An event that is triggered by beginPage(). Event name: 'beginPage'. | View |
EVENT_END_PAGE | Event | An event that is triggered by endPage(). Event name: 'endPage'. | View |
EVENT_BEFORE_RENDER | ViewEvent | An event that is triggered by renderFile() right before it renders a view file. Event name: 'beforeRender'. | View |
EVENT_AFTER_RENDER | ViewEvent | An event that is triggered by renderFile() right after it renders a view file. Event name: 'afterRender'. | View |
Property details
Section titled “Property details”$blocks
Section titled “$blocks ”public array $blocks = nullA list of named output blocks.
The keys are the block names and the values
are the corresponding block content. You can call beginBlock() and endBlock()
to capture small fragments of a view. They can be later accessed somewhere else
through this property.
$defaultExtension
Section titled “$defaultExtension ”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
Section titled “$renderers ”public array|null $renderers = nullA 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().
$theme
Section titled “$theme ”public yii\base\Theme|array|string|null $theme = nullThe theme object or the configuration for creating the theme object.
If not set, it means theming is not enabled.
$viewFile
Section titled “$viewFile ”public string|bool $viewFileThe view file currently being rendered.
False if no view file is being rendered.
Access
Read-only (virtual property).
Method details
Section titled “Method details”addDynamicPlaceholder()
Section titled “addDynamicPlaceholder() ”public function addDynamicPlaceholder(mixed $placeholder, mixed $statements): mixedAdds 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()
Section titled “afterRender() ”public function afterRender(string $viewFile, array $params, string &$output): mixedThis method is invoked right after renderFile() renders a view file.
The default implementation will trigger the 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() method. |
$output | string | The rendering result of the view file. Updates to this parameter will be passed back and returned by renderFile(). |
Returns
mixed
beforeRender()
Section titled “beforeRender() ”public function beforeRender(string $viewFile, array $params): boolThis method is invoked right before renderFile() renders a view file.
The default implementation will trigger the 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() method. |
Returns
bool: Whether to continue rendering the view file.
beginBlock()
Section titled “beginBlock() ”public function beginBlock(string $id, bool $renderInPlace = false): yii\widgets\BlockBegins recording a block.
This method is a shortcut to beginning Block.
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: The Block widget instance
beginCache()
Section titled “beginCache() ”public function beginCache(string $id, array $properties = []): boolBegins fragment caching.
This method will display cached content if it is available.
If not, it will start caching and would expect an 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 |
Returns
bool: Whether you should generate the content for caching. False if the cached version is available.
beginContent()
Section titled “beginContent() ”public function beginContent(string $viewFile, array $params = []): yii\widgets\ContentDecoratorBegins 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. |
$params | array | The variables (name => value) to be extracted and made available in the decorative view. |
Returns
yii\widgets\ContentDecorator: The ContentDecorator widget instance
See also
beginPage()
Section titled “beginPage() ”public function beginPage(): mixedMarks the beginning of a page.
Returns
mixed
endBlock()
Section titled “endBlock() ”public function endBlock(): mixedEnds recording a block.
Returns
mixed
endCache()
Section titled “endCache() ”public function endCache(): mixedEnds fragment caching.
Returns
mixed
endContent()
Section titled “endContent() ”public function endContent(): mixedEnds the rendering of content.
Returns
mixed
endPage()
Section titled “endPage() ”public function endPage(): mixedMarks the ending of a page.
Returns
mixed
evaluateDynamicContent()
Section titled “evaluateDynamicContent() ”public function evaluateDynamicContent(string $statements): mixedEvaluates 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()
Section titled “getDynamicContents() ”public function getDynamicContents(): yii\base\DynamicContentAwareInterface[]Returns a list of currently active dynamic content class instances.
getDynamicPlaceholders()
Section titled “getDynamicPlaceholders() ”public function getDynamicPlaceholders(): arrayReturns 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()
Section titled “getViewFile() ”public function getViewFile(): string|boolReturns
string|bool: The view file currently being rendered. False if no view file is being rendered.
init()
Section titled “init() ”public function init(): mixedInitializes the view component.
Returns
mixed
popDynamicContent()
Section titled “popDynamicContent() ”public function popDynamicContent(): mixedRemoves a last class instance supporting dynamic contents from a list of currently active dynamic content class instances.
Returns
mixed
Since
2.0.14
pushDynamicContent()
Section titled “pushDynamicContent() ”public function pushDynamicContent(yii\base\DynamicContentAwareInterface $instance): mixedAdds 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 | Class instance supporting dynamic contents. |
Returns
mixed
Since
2.0.14
render()
Section titled “render() ”public function render(string $view, array $params = [], object|null $context = null): stringRenders a view.
The view to be rendered can be specified in one of the following formats:
- path alias (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 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 of the current 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 of the view$context. If$contextis 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| | The context to be assigned to the view and can later be accessed via $context in the view. If the context implements ViewContextInterface, it may also be used to locate the view file corresponding to a relative view name. |
Returns
string: The rendering result
Throws
ViewNotFoundException: if the view file does not exist.InvalidCallException: if the view cannot be resolved.
See also
renderDynamic()
Section titled “renderDynamic() ”public function renderDynamic(string $statements): stringRenders 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
renderFile()
Section titled “renderFile() ”public function renderFile(string $viewFile, array $params = [], object|null $context = null): stringRenders a view file.
If $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() to localize the view file.
If renderer 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| | The context that the view should use for rendering the view. If null, existing $context will be used. |
Returns
string: The rendering result
Throws
ViewNotFoundException: if the view file does not exist
renderPhpFile()
Section titled “renderPhpFile() ”public function renderPhpFile(string $_file_, array $_params_ = []): stringRenders 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().
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
setDynamicPlaceholders()
Section titled “setDynamicPlaceholders() ”public function setDynamicPlaceholders(mixed $placeholders): mixedSets 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()
Section titled “findViewFile() ”protected function findViewFile(string $view, object|null $context = null): stringFinds the view file based on the given view name.
Parameters
| Parameter | Type | Description |
|---|---|---|
$view | string | The view name or the path alias of the view file. Please refer to render() on how to specify this parameter. |
$context | object| | The context to be assigned to the view and can later be accessed via $context in the view. If the context implements ViewContextInterface, 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: if a relative view name is given while there is no active context to determine the corresponding view file.
getRequestedViewFile()
Section titled “getRequestedViewFile() ”protected function getRequestedViewFile(): string|boolReturns
string|bool: The requested view currently being rendered. False if no view file is being rendered.
Since
2.0.16