没有命名空间的类 yii yii\base yii\behaviors yii\caching yii\captcha yii\console yii\console\controllers yii\console\widgets yii\data yii\db yii\db\conditions yii\db\cubrid yii\db\cubrid\conditions yii\db\mssql yii\db\mssql\conditions yii\db\mysql yii\db\oci yii\db\oci\conditions yii\db\pgsql yii\db\sqlite yii\db\sqlite\conditions yii\di yii\filters yii\filters\auth yii\grid yii\helpers yii\i18n yii\log yii\mail yii\mutex yii\rbac yii\rest yii\test yii\validators yii\web yii\widgets

Class yii\base\View

继承yii\base\View » yii\base\Component » yii\base\BaseObject
实现yii\base\Configurable, yii\base\DynamicContentAwareInterface
子类yii\web\View
可用版本自2.0
源码 https://github.com/yiichina/yii2/blob/api/framework/base/View.php

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.

公共属性

隐藏继承的属性

属性类型描述被定义在
$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|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
$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
$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
$viewFile string|boolean The view file currently being rendered. yii\base\View

公共方法

隐藏继承的方法

方法描述被定义在
__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
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
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
endCache() Ends fragment caching. yii\base\View
endContent() Ends the rendering of content. yii\base\View
endPage() Marks the ending of a page. yii\base\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
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
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
render() Renders a view. yii\base\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
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

受保护的方法

隐藏继承的方法

方法描述被定义在
findViewFile() Finds the view file based on the given view name. yii\base\View
getRequestedViewFile() yii\base\View

Events

隐藏继承的事件

事件类型描述被定义在
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_PAGE yii\base\Event An event that is triggered by beginPage(). yii\base\View
EVENT_END_PAGE yii\base\Event An event that is triggered by endPage(). yii\base\View

属性详情

$blocks 公共 属性

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

public array $blocks null
$cacheStack 公共 属性
Deprecated Since 2.0.14. Do not use this property directly. Use methods getDynamicContents(), pushDynamicContent(), popDynamicContent() instead.

A list of currently active dynamic content class instances. This property is used internally to implement the dynamic content caching feature. Do not modify it directly.

$context 公共 属性

The context under which the renderFile() method is being invoked.

$defaultExtension 公共 属性

The default view file extension. This will be appended to view file names if they don't have file extensions.

public string $defaultExtension 'php'
$dynamicContents 公共 只读 属性

Class instances supporting dynamic contents.

$dynamicPlaceholders 公共 属性
Deprecated Since 2.0.14. Do not use this property directly. Use methods getDynamicPlaceholders(), setDynamicPlaceholders(), addDynamicPlaceholder() instead.

A list of placeholders for embedding dynamic contents. This property is used internally to implement the content caching feature. Do not modify it directly.

public array getDynamicPlaceholders ( )
public void setDynamicPlaceholders ( $placeholders )
$params 公共 属性

Custom parameters that are shared among view templates.

public mixed $params = []
$renderers 公共 属性

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

public array $renderers null
$theme 公共 属性

The theme object or the configuration for creating the theme object. If not set, it means theming is not enabled.

$viewFile 公共 只读 属性

The view file currently being rendered. False if no view file is being rendered.

方法详情

addDynamicPlaceholder() 公共 方法

Adds a placeholder for dynamic content.

This method is used internally to implement the content caching feature.

public void addDynamicPlaceholder($placeholder, $statements)
$placeholder
$statements string

The PHP statements for generating the dynamic content.

afterRender() 公共 方法

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

public void afterRender($viewFile, $params, &$output)
$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().

beforeRender() 公共 方法

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

public boolean beforeRender($viewFile, $params)
$viewFile string

The view file to be rendered.

$params array

The parameter array passed to the render() method.

return boolean

Whether to continue rendering the view file.

beginBlock() 公共 方法

Begins recording a block.

This method is a shortcut to beginning yii\widgets\Block.

public yii\widgets\Block beginBlock($id, $renderInPlace false)
$id string

The block ID.

$renderInPlace boolean

Whether to render the block content in place. Defaults to false, meaning the captured block will not be displayed.

return yii\widgets\Block

The Block widget instance

beginCache() 公共 方法

Begins 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();
}
public boolean beginCache($id, $properties = [])
$id string

A unique ID identifying the fragment to be cached.

$properties array

Initial property values for yii\widgets\FragmentCache

return boolean

Whether you should generate the content for caching. False if the cached version is available.

beginContent() 公共 方法

Begins 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(); ?>

参见 yii\widgets\ContentDecorator.

public yii\widgets\ContentDecorator beginContent($viewFile, $params = [])
$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.

return yii\widgets\ContentDecorator

The ContentDecorator widget instance

beginPage() 公共 方法

Marks the beginning of a page.

public void beginPage()
endBlock() 公共 方法

Ends recording a block.

public void endBlock()
endCache() 公共 方法

Ends fragment caching.

public void endCache()
endContent() 公共 方法

Ends the rendering of content.

public void endContent()
endPage() 公共 方法

Marks the ending of a page.

public void endPage()
evaluateDynamicContent() 公共 方法

Evaluates the given PHP statements.

This method is mainly used internally to implement dynamic content feature.

public mixed evaluateDynamicContent($statements)
$statements string

The PHP statements to be evaluated.

return mixed

The return value of the PHP statements.

findViewFile() 受保护 方法

Finds the view file based on the given view name.

protected string findViewFile($view, $context null)
$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 yii\base\ViewContextInterface, it may also be used to locate the view file corresponding to a relative view name.

return string

The view file path. Note that the file may not exist.

throws yii\base\InvalidCallException

if a relative view name is given while there is no active context to determine the corresponding view file.

getDynamicContents() 公共 方法 (自版本 2.0.14 可用)

Returns a list of currently active dynamic content class instances.

public yii\base\DynamicContentAwareInterface[] getDynamicContents()
return yii\base\DynamicContentAwareInterface[]

Class instances supporting dynamic contents.

getDynamicPlaceholders() 公共 方法

Returns a list of placeholders for dynamic content. This method is used internally to implement the content caching feature.

public array getDynamicPlaceholders()
return array

A list of placeholders.

getRequestedViewFile() 受保护 方法 (自版本 2.0.16 可用)

protected string|boolean getRequestedViewFile()
return string|boolean

The requested view currently being rendered. False if no view file is being rendered.

getViewFile() 公共 方法

public string|boolean getViewFile()
return string|boolean

The view file currently being rendered. False if no view file is being rendered.

init() 公共 方法

Initializes the view component.

public void init()
popDynamicContent() 公共 方法 (自版本 2.0.14 可用)

Removes a last class instance supporting dynamic contents from a list of currently active dynamic content class instances.

public void popDynamicContent()
pushDynamicContent() 公共 方法 (自版本 2.0.14 可用)

Adds a class instance supporting dynamic contents to the end of a list of currently active dynamic content class instances.

public void pushDynamicContent(yii\base\DynamicContentAwareInterface $instance)
$instance yii\base\DynamicContentAwareInterface

Class instance supporting dynamic contents.

render() 公共 方法

Renders 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 $context is 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).

参见 renderFile().

public string render($view, $params = [], $context null)
$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 yii\base\ViewContextInterface, it may also be used to locate the view file corresponding to a relative view name.

return string

The rendering result

throws yii\base\ViewNotFoundException

if the view file does not exist.

throws yii\base\InvalidCallException

if the view cannot be resolved.

renderDynamic() 公共 方法

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

public string renderDynamic($statements)
$statements string

The PHP statements for generating the dynamic content.

return string

The placeholder of the dynamic content, or the dynamic content if there is no active content cache currently.

renderFile() 公共 方法

Renders 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 yii\helpers\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.

public string renderFile($viewFile, $params = [], $context null)
$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.

return string

The rendering result

throws yii\base\ViewNotFoundException

if the view file does not exist

renderPhpFile() 公共 方法

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

public string renderPhpFile($_file_, $_params_ = [])
$_file_ string

The view file.

$_params_ array

The parameters (name-value pairs) that will be extracted and made available in the view file.

return string

The rendering result

throws Exception
throws Throwable
setDynamicPlaceholders() 公共 方法

Sets a list of placeholders for dynamic content. This method is used internally to implement the content caching feature.

public void setDynamicPlaceholders($placeholders)
$placeholders array

A list of placeholders.

事件详情

EVENT_AFTER_RENDER event of type yii\base\ViewEvent

An event that is triggered by renderFile() right after it renders a view file.

EVENT_BEFORE_RENDER event of type yii\base\ViewEvent

An event that is triggered by renderFile() right before it renders a view file.

EVENT_BEGIN_PAGE event of type yii\base\Event

An event that is triggered by beginPage().

EVENT_END_PAGE event of type yii\base\Event

An event that is triggered by endPage().