PHP Class yii\base\View

View provides a set of methods (e.g. View::render) for rendering purpose.
Since: 2.0
Author: Qiang Xue ([email protected])
Inheritance: extends Component
Show file Open project: yiisoft/yii2 Class Usage Examples

Public Properties

Property Type Description
$blocks a list of named output blocks. The keys are the block names and the values are the corresponding block content. You can call View::beginBlock and View::endBlock to capture small fragments of a view. They can be later accessed somewhere else through this property.
$cacheStack a list of currently active fragment cache widgets. This property is used internally to implement the content caching feature. Do not modify it directly.
$context the context under which the View::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.
$dynamicPlaceholders a list of placeholders for embedding dynamic contents. This property is used internally to implement the content caching feature. Do not modify it directly.
$params custom parameters that are shared among view templates.
$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: php [ '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 View::renderPhpFile.
$theme the theme object or the configuration for creating the theme object. If not set, it means theming is not enabled.

Public Methods

Method Description
addDynamicPlaceholder ( string $placeholder, string $statements ) Adds a placeholder for dynamic content.
afterRender ( string $viewFile, array $params, string &$output ) This method is invoked right after View::renderFile renders a view file.
beforeRender ( string $viewFile, array $params ) : boolean This method is invoked right before View::renderFile renders a view file.
beginBlock ( string $id, boolean $renderInPlace = false ) : Block Begins recording a block.
beginCache ( string $id, array $properties = [] ) : boolean Begins fragment caching.
beginContent ( string $viewFile, array $params = [] ) : yii\widgets\ContentDecorator Begins the rendering of content that is to be decorated by the specified view.
beginPage ( ) Marks the beginning of a page.
endBlock ( ) Ends recording a block.
endCache ( ) Ends fragment caching.
endContent ( ) Ends the rendering of content.
endPage ( ) Marks the ending of a page.
evaluateDynamicContent ( string $statements ) : mixed Evaluates the given PHP statements.
getViewFile ( ) : string | boolean
init ( ) Initializes the view component.
render ( string $view, array $params = [], object $context = null ) : string Renders a view.
renderDynamic ( string $statements ) : string Renders dynamic content returned by the given PHP statements.
renderFile ( string $viewFile, array $params = [], object $context = null ) : string Renders a view file.
renderPhpFile ( string $_file_, array $_params_ = [] ) : string Renders a view file as a PHP script.

Protected Methods

Method Description
findViewFile ( string $view, object $context = null ) : string Finds the view file based on the given view name.

Method Details

addDynamicPlaceholder() public method

This method is internally used.
public addDynamicPlaceholder ( string $placeholder, string $statements )
$placeholder string the placeholder name
$statements string the PHP statements for generating the dynamic content

afterRender() public method

The default implementation will trigger the [[EVENT_AFTER_RENDER]] event. If you override this method, make sure you call the parent implementation first.
public afterRender ( string $viewFile, array $params, string &$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() public method

The default implementation will trigger the [[EVENT_BEFORE_RENDER]] event. If you override this method, make sure you call the parent implementation first.
public beforeRender ( string $viewFile, array $params ) : boolean
$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() public method

This method is a shortcut to beginning [[Block]]
public beginBlock ( string $id, boolean $renderInPlace = false ) : Block
$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() public method

This method will display cached content if it is available. If not, it will start caching and would expect an View::endCache call to end the cache and save the content into cache. A typical usage of fragment caching is as follows, php if ($this->beginCache($id)) { ...generate content here $this->endCache(); }
public beginCache ( string $id, array $properties = [] ) : boolean
$id string a unique ID identifying the fragment to be cached.
$properties array initial property values for [[FragmentCache]]
return boolean whether you should generate the content for caching. False if the cached version is available.

beginContent() public method

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 beginContent('@app/views/layouts/base.php'); ?> ...layout content here... endContent(); ?>
See also: ContentDecorator
public beginContent ( string $viewFile, array $params = [] ) : yii\widgets\ContentDecorator
$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() public method

Marks the beginning of a page.
public beginPage ( )

endBlock() public method

Ends recording a block.
public endBlock ( )

endCache() public method

Ends fragment caching.
public endCache ( )

endContent() public method

Ends the rendering of content.
public endContent ( )

endPage() public method

Marks the ending of a page.
public endPage ( )

evaluateDynamicContent() public method

This method is mainly used internally to implement dynamic content feature.
public evaluateDynamicContent ( string $statements ) : mixed
$statements string the PHP statements to be evaluated.
return mixed the return value of the PHP statements.

findViewFile() protected method

Finds the view file based on the given view name.
protected findViewFile ( string $view, object $context = null ) : string
$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.
return string the view file path. Note that the file may not exist.

getViewFile() public method

public getViewFile ( ) : string | boolean
return string | boolean the view file currently being rendered. False if no view file is being rendered.

init() public method

Initializes the view component.
public init ( )

render() public method

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 [[Application::viewPath|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 [[Module::viewPath|view path]] of the [[Controller::module|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 [[ViewContextInterface::getViewPath()|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).
See also: renderFile()
public render ( string $view, array $params = [], object $context = null ) : string
$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.
return string the rendering result

renderDynamic() public method

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 renderDynamic ( string $statements ) : string
$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() public method

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 [[renderers|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 renderFile ( string $viewFile, array $params = [], object $context = null ) : string
$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

renderPhpFile() public method

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 View::renderFile.
public renderPhpFile ( string $_file_, array $_params_ = [] ) : string
$_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

Property Details

$blocks public property

a list of named output blocks. The keys are the block names and the values are the corresponding block content. You can call View::beginBlock and View::endBlock to capture small fragments of a view. They can be later accessed somewhere else through this property.
public $blocks

$cacheStack public property

a list of currently active fragment cache widgets. This property is used internally to implement the content caching feature. Do not modify it directly.
public $cacheStack

$context public property

the context under which the View::renderFile method is being invoked.
public $context

$defaultExtension public property

the default view file extension. This will be appended to view file names if they don't have file extensions.
public $defaultExtension

$dynamicPlaceholders public property

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

$params public property

custom parameters that are shared among view templates.
public $params

$renderers public property

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: php [ '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 View::renderPhpFile.
public $renderers

$theme public property

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