Inheritance | yii\web\View » yii\base\View » yii\base\Component » yii\base\Object |
---|---|
Implements | yii\base\Configurable |
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.
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. | yii\web\View |
$behaviors | yii\base\Behavior[] | List of behaviors attached to this component | yii\base\Component |
$blocks | array | A list of named output blocks. | yii\base\View |
$cacheStack | array | A list of currently active fragment cache widgets. | 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 |
$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 | mixed | 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. | yii\base\View |
Method | Description | Defined By |
---|---|---|
__call() | Calls the named method which is not a class method. | yii\base\Object |
__clone() | This method is called after the object is created by cloning an existing one. | yii\base\Component |
__construct() | Constructor. | yii\base\Object |
__get() | Returns the value of an object property. | yii\base\Object |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\Object |
__set() | Sets value of an object property. | yii\base\Object |
__unset() | Sets an object property to null. | yii\base\Object |
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\Object |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\Object |
className() | Returns the fully qualified name of this class. | yii\base\Object |
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 |
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\Object |
hasProperty() | Returns a value indicating whether a property is defined. | yii\base\Object |
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 |
registerAssetBundle() | Registers the named asset bundle. | 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 |
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 |
trigger() | Triggers an event. | yii\base\Component |
Method | Description | Defined By |
---|---|---|
findViewFile() | Finds the view file based on the given view name. | 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 |
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 |
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 |
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 = []
The asset manager. Defaults to the "assetManager" application component.
public yii\web\AssetManager getAssetManager ( )
public void setAssetManager ( $value )
The registered CSS code blocks.
See also registerCss().
public array $css = []
The registered CSS files.
See also registerCssFile().
public array $cssFiles = []
The registered JS code blocks
See also registerJs().
public array $js = []
The registered JS files.
See also registerJsFile().
public array $jsFiles = []
The registered link tags.
See also registerLinkTag().
public array $linkTags = []
The registered meta tags.
See also registerMetaTag().
public array $metaTags = []
The page title
public string $title = null
Marks the beginning of an HTML body section.
public void beginBody ( ) |
---|
Clears up the registered meta tags, link tags, css/js scripts and files.
public void clear ( ) |
---|
Marks the ending of an HTML body section.
public void endBody ( ) |
---|
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. |
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. |
Marks the position of an HTML head section.
public void head ( ) |
---|
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 |
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 |
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 |
$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. |
Registers a CSS file.
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:
|
$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. |
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:
|
$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. |
Registers a JS file.
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:
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. |
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. |
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. |
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 |
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 |
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 |
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 |
Sets the asset manager.
public void setAssetManager ( $value ) | ||
---|---|---|
$value | yii\web\AssetManager |
The asset manager |
An event that is triggered by beginBody().
An event that is triggered by endBody().
© 2008–2017 by Yii Software LLC
Licensed under the three clause BSD license.
http://www.yiiframework.com/doc-2.0/yii-web-view.html