继承yii\web\View » yii\base\View » yii\base\Component » yii\base\Object
可用自版本2.0

View represents a view object in the MVC pattern.

View provides a set of methods (e.g. \yii\web\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.

公共属性

隐藏继承属性

属性类型描述定义在
$assetBundles yii\web\AssetBundle[] List of the registered asset bundles. yii\web\View
$css array The registered CSS code blocks. yii\web\View
$cssFiles array The registered CSS files. yii\web\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
$title string The page title yii\web\View

公共方法

隐藏继承方法

方法描述定义在
beginBody() Marks the beginning of an HTML body section. yii\web\View
clear() Clears up the registered meta tags, link tags, css/js scripts and files. yii\web\View
endBody() Marks the ending of an HTML body section. yii\web\View
endPage() Marks the ending of an HTML page. yii\web\View
getAssetManager() Registers the asset manager being used by this view object. yii\web\View
head() Marks the position of an HTML head section. yii\web\View
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
renderAjax() Renders a view in response to an AJAX request. yii\web\View
setAssetManager() Sets the asset manager. yii\web\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_BEGIN_BODY \yii\web\Event An event that is triggered by beginBody(). yii\web\View
EVENT_END_BODY \yii\web\Event An event that is triggered by endBody(). yii\web\View

常量

隐藏继承常量

常量描述定义在
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

属性详情

$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.

参见 registerAssetBundle().

$css public property

The registered CSS code blocks.

参见 registerCss().

public array $css null
$cssFiles public property

The registered CSS files.

参见 registerCssFile().

public array $cssFiles null
$js public property

The registered JS code blocks

参见 registerJs().

public array $js null
$jsFiles public property

The registered JS files.

参见 registerJsFile().

public array $jsFiles null
$linkTags public property

The registered link tags.

参见 registerLinkTag().

public array $linkTags null
$metaTags public property

The registered meta tags.

参见 registerMetaTag().

public array $metaTags null
$title public property

The page title

public string $title null

方法详情

beginBody() public 方法

Marks the beginning of an HTML body section.

public void beginBody ( )
clear() public 方法

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

public void clear ( )
endBody() public 方法

Marks the ending of an HTML body section.

public void endBody ( )
endPage() public 方法

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 方法

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 方法

Marks the position of an HTML head section.

public void head ( )
registerAssetBundle() public 方法

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 方法

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

registerCss() public 方法

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 方法

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:

  • depends: array, specifies the names of the asset bundles that this CSS file depends on.
$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.

registerJs() public 方法

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 方法

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:

  • 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.

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.

registerLinkTag() public 方法

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 方法

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 方法

Renders a view in response to an AJAX request.

This method is similar to \yii\web\render() except that it will surround the view being rendered with the calls of \yii\web\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.

参见 \yii\web\render().

public string renderAjax ( $view, $params = [], $context null )
$view string

The view name. Please refer to \yii\web\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 \yii\web\context will be used.

return string

The rendering result

renderBodyBeginHtml() protected 方法

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 方法

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 方法

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 方法

Sets the asset manager.

public void setAssetManager ( $value )
$value yii\web\AssetManager

The asset manager

事件详情

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().