Class View
View, the V in the MVC triad. View interacts with Helpers and view variables passed in from the controller to render the results of the controller action. Often this is HTML, but can also take the form of JSON, XML, PDF's or streaming files.
CakePHP uses a two-step-view pattern. This means that the template content is rendered first,
and then inserted into the selected layout. This also means you can pass data from the template to the
layout using $this->set()
View class supports using plugins as themes. You can set
public function beforeRender(\Cake\Event\EventInterface $event)
{
$this->viewBuilder()->setTheme('SuperHot');
}
in your Controller to use plugin SuperHot
as a theme. Eg. If current action
is PostsController::index() then View class will look for template file
plugins/SuperHot/templates/Posts/index.php
. If a theme template
is not found for the current action the default app template file is used.
Constants
-
string
NAME_TEMPLATE ¶'templates'
Constant for type used for App::path().
-
string
PLUGIN_TEMPLATE_FOLDER ¶'plugin'
Constant for folder name containing files for overriding plugin templates.
-
string
TYPE_ELEMENT ¶'element'
Constant for view file type 'element'
-
string
TYPE_LAYOUT ¶'layout'
Constant for view file type 'layout'
-
string
TYPE_MATCH_ALL ¶'_match_all_'
The magic 'match-all' content type that views can use to behave as a fallback during content-type negotiation.
-
string
TYPE_TEMPLATE ¶'template'
Constant for view file type 'template'.
Property Summary
-
$Blocks public @property
Cake\View\ViewBlock
-
$Breadcrumbs public @property
Cake\View\Helper\BreadcrumbsHelper
-
$Flash public @property
Cake\View\Helper\FlashHelper
-
$Form public @property
Cake\View\Helper\FormHelper
-
$Html public @property
Cake\View\Helper\HtmlHelper
-
$Number public @property
Cake\View\Helper\NumberHelper
-
$Paginator public @property
Cake\View\Helper\PaginatorHelper
-
$Text public @property
Cake\View\Helper\TextHelper
-
$Time public @property
Cake\View\Helper\TimeHelper
-
$Url public @property
Cake\View\Helper\UrlHelper
-
$_config protected
array<string, mixed>
Runtime config
-
$_configInitialized protected
bool
Whether the config property has already been configured with defaults
-
$_current protected
string
The currently rendering view file. Used for resolving parent files.
-
$_currentType protected
string
Currently rendering an element. Used for finding parent fragments for elements.
-
$_defaultConfig protected
array<string, mixed>
Default custom config options.
-
$_eventClass protected
string
Default class name for new event objects.
-
$_eventManager protected
Cake\Event\EventManagerInterface|null
Instance of the Cake\Event\EventManager this object is using to dispatch inner events.
-
$_ext protected
string
File extension. Defaults to ".php".
-
$_helpers protected
Cake\View\HelperRegistry|null
Helpers collection
-
$_parents protected
array<string, string>
The names of views and their parents used with View::extend();
-
$_passedVars protected
list<string>
List of variables to collect from the associated controller.
-
$_paths protected
list<string>
Holds an array of paths.
-
$_pathsForPlugin protected
array<string, list<string>>
Holds an array of plugin paths.
-
$_stack protected
list<string>
Content stack, used for nested templates that all use View::extend();
-
$_viewBlockClass protected
string
ViewBlock class.
-
$autoLayout protected
bool
Turns on or off CakePHP's conventional mode of applying layout files. On by default. Setting to off means that layouts will not be automatically applied to rendered templates.
-
$elementCache protected
string
The Cache configuration View will use to store cached elements. Changing this will change the default configuration elements are stored under. You can also choose a cache config per element.
-
$helpers protected
array<string, array<string, mixed>>
A configuration array for helpers to be loaded.
-
$layout protected
string
The name of the layout file to render the template inside of. The name specified is the filename of the layout in
templates/layout/
without the .php extension. -
$layoutPath protected
string
The name of the layouts subfolder containing layouts for this View.
-
$name protected
string
Name of the controller that created the View if any.
-
$plugin protected
string|null
The name of the plugin.
-
$request protected
Cake\Http\ServerRequest
An instance of a \Cake\Http\ServerRequest object that contains information about the current request. This object contains all the information about a request and several methods for reading additional information about the request.
-
$response protected
Cake\Http\Response
Reference to the Response object
-
$subDir protected
string
Sub-directory for this template file. This is often used for extension based routing. Eg. With an
xml
extension, $subDir would bexml/
-
$template protected
string
The name of the template file to render. The name specified is the filename in
templates/<SubFolder>/
without the .php extension. -
$templatePath protected
string
The name of the subfolder containing templates for this View.
-
$theme protected
string|null
The view theme to use.
-
$viewVars protected
array<string, mixed>
An array of variables
Method Summary
-
__construct() public
Constructor
-
__get() public
Magic accessor for helpers.
-
_checkFilePath() protected
Check that a view file path does not go outside of the defined template paths.
-
_configDelete() protected
Deletes a single config key.
-
_configRead() protected
Reads a config key.
-
_configWrite() protected
Writes a config key.
-
_createCell() protected
Create and configure the cell instance.
-
_elementCache() protected
Generate the cache configuration options for an element.
-
_evaluate() protected
Sandbox method to evaluate a template / view script in.
-
_getElementFileName() protected
Finds an element filename, returns false on failure.
-
_getLayoutFileName() protected
Returns layout filename for this template as a string.
-
_getSubPaths() protected
Find all sub templates path, based on $basePath If a prefix is defined in the current request, this method will prepend the prefixed template path to the $basePath, cascading up in case the prefix is nested. This is essentially used to find prefixed template paths for elements and layouts.
-
_getTemplateFileName() protected
Returns filename of given action's template file as a string. CamelCased action names will be under_scored by default. This means that you can have LongActionNames that refer to long_action_names.php templates. You can change the inflection rule by overriding _inflectTemplateFileName.
-
_inflectTemplateFileName() protected
Change the name of a view template file into underscored format.
-
_paths() protected
Return all possible paths to find view files in order
-
_render() protected
Renders and returns output for given template filename with its array of data. Handles parent/extended templates.
-
_renderElement() protected
Renders an element and fires the before and afterRender callbacks for it and writes to the cache if a cache is used
-
addHelper() protected
Adds a helper from within
initialize()
method. -
append() public
Append to an existing or new block.
-
assign() public
Set the content for a block. This will overwrite any existing content.
-
blocks() public
Get the names of all the existing blocks.
-
cache() public
Create a cached block of view logic.
-
cell() protected
Renders the given cell.
-
configShallow() public
Merge provided config with existing config. Unlike
config()
which does a recursive merge for nested keys, this method does a simple merge. -
contentType() public static
Mime-type this view class renders as.
-
disableAutoLayout() public
Turns off CakePHP's conventional mode of applying layout files. Layouts will not be automatically applied to rendered views.
-
dispatchEvent() public
Wrapper for creating and dispatching events.
-
element() public
Renders a piece of PHP with provided parameters and returns HTML, XML, or any other string.
-
elementExists() public
Checks if an element exists
-
enableAutoLayout() public
Turns on or off CakePHP's conventional mode of applying layout files. On by default. Setting to off means that layouts will not be automatically applied to rendered views.
-
end() public
End a capturing block. The compliment to View::start()
-
exists() public
Check if a block exists
-
extend() public
Provides template or element extension/inheritance. Templates can extends a parent template and populate blocks in the parent template.
-
fetch() public
Fetch the content for a block. If a block is empty or undefined '' will be returned.
-
get() public
Returns the contents of the given View variable.
-
getConfig() public
Returns the config.
-
getConfigOrFail() public
Returns the config for this specific key.
-
getCurrentType() public
Retrieve the current template type
-
getElementPaths() protected
Get an iterator for element paths.
-
getEventManager() public
Returns the Cake\Event\EventManager manager instance for this object.
-
getLayout() public
Get the name of the layout file to render the template inside of. The name specified is the filename of the layout in
templates/layout/
without the .php extension. -
getLayoutPath() public
Get path for layout files.
-
getLayoutPaths() protected
Get an iterator for layout paths.
-
getName() public
Returns the View's controller name.
-
getPlugin() public
Returns the plugin name.
-
getRequest() public
Gets the request instance.
-
getResponse() public
Gets the response instance.
-
getSubDir() public
Get sub-directory for this template files.
-
getTemplate() public
Get the name of the template file to render. The name specified is the filename in
templates/<SubFolder>/
without the .php extension. -
getTemplatePath() public
Get path for templates files.
-
getTheme() public
Get the current view theme.
-
getVars() public
Returns a list of variables available in the current View context
-
helpers() public
Get the helper registry in use by this View class.
-
initialize() public
Initialization hook method.
-
isAutoLayoutEnabled() public
Returns if CakePHP's conventional mode of applying layout files is enabled. Disabled means that layouts will not be automatically applied to rendered views.
-
loadHelper() public
Loads a helper. Delegates to the
HelperRegistry::load()
to load the helper. -
loadHelpers() public
Interact with the HelperRegistry to load all the helpers.
-
log() public
Convenience method to write a message to Log. See Log::write() for more information on writing to logs.
-
pluginSplit() public
Splits a dot syntax plugin name into its plugin and filename. If $name does not have a dot, then index 0 will be null. It checks if the plugin is loaded, else filename will stay unchanged for filenames containing dot
-
prepend() public
Prepend to an existing or new block.
-
render() public
Renders view for given template file and layout.
-
renderLayout() public
Renders a layout. Returns output from _render().
-
reset() public
Reset the content for a block. This will overwrite any existing content.
-
set() public
Saves a variable or an associative array of variables for use inside a template.
-
setConfig() public
Sets the config.
-
setContentType() protected
Set the response content-type based on the view's contentType()
-
setElementCache() public
Set The cache configuration View will use to store cached elements
-
setEventManager() public
Returns the Cake\Event\EventManagerInterface instance for this object.
-
setLayout() public
Set the name of the layout file to render the template inside of. The name specified is the filename of the layout in
templates/layout/
without the .php extension. -
setLayoutPath() public
Set path for layout files.
-
setPlugin() public
Sets the plugin name.
-
setRequest() public
Sets the request objects and configures a number of controller properties based on the contents of the request. The properties that get set are:
-
setResponse() public
Sets the response instance.
-
setSubDir() public
Set sub-directory for this template files.
-
setTemplate() public
Set the name of the template file to render. The name specified is the filename in
templates/<SubFolder>/
without the .php extension. -
setTemplatePath() public
Set path for templates files.
-
setTheme() public
Set the view theme to use.
-
start() public
Start capturing output for a 'block'
Method Detail
__construct() ¶ public
__construct(Cake\Http\ServerRequest|null $request = null, Cake\Http\Response|null $response = null, Cake\Event\EventManagerInterface|null $eventManager = null, array<string, mixed> $viewOptions = [])
Constructor
Parameters
-
Cake\Http\ServerRequest|null
$request optional Request instance.
-
Cake\Http\Response|null
$response optional Response instance.
-
Cake\Event\EventManagerInterface|null
$eventManager optional Event manager instance.
-
array<string, mixed>
$viewOptions optional View options. See {@link View::$_passedVars} for list of options which get set as class properties.
__get() ¶ public
__get(string $name): Cake\View\Helper|null
Magic accessor for helpers.
Parameters
-
string
$name Name of the attribute to get.
Returns
Cake\View\Helper|null
_checkFilePath() ¶ protected
_checkFilePath(string $file, string $path): string
Check that a view file path does not go outside of the defined template paths.
Only paths that contain ..
will be checked, as they are the ones most likely to
have the ability to resolve to files outside of the template paths.
Parameters
-
string
$file The path to the template file.
-
string
$path Base path that $file should be inside of.
Returns
string
Throws
InvalidArgumentException
_configDelete() ¶ protected
_configDelete(string $key): void
Deletes a single config key.
Parameters
-
string
$key Key to delete.
Returns
void
Throws
Cake\Core\Exception\CakeException
if attempting to clobber existing config
_configRead() ¶ protected
_configRead(string|null $key): mixed
Reads a config key.
Parameters
-
string|null
$key Key to read.
Returns
mixed
_configWrite() ¶ protected
_configWrite(array<string, mixed>|string $key, mixed $value, string|bool $merge = false): void
Writes a config key.
Parameters
-
array<string, mixed>|string
$key Key to write to.
-
mixed
$value Value to write.
-
string|bool
$merge optional True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.
Returns
void
Throws
Cake\Core\Exception\CakeException
if attempting to clobber existing config
_createCell() ¶ protected
_createCell(string $className, string $action, string|null $plugin, array<string, mixed> $options): Cake\View\Cell
Create and configure the cell instance.
Parameters
-
string
$className The cell classname.
-
string
$action The action name.
-
string|null
$plugin The plugin name.
-
array<string, mixed>
$options The constructor options for the cell.
Returns
Cake\View\Cell
_elementCache() ¶ protected
_elementCache(string $name, array $data, array<string, mixed> $options): array<string, mixed>
Generate the cache configuration options for an element.
Parameters
-
string
$name Element name
-
array
$data Data
-
array<string, mixed>
$options Element options
Returns
array<string, mixed>
_evaluate() ¶ protected
_evaluate(string $templateFile, array $dataForView): string
Sandbox method to evaluate a template / view script in.
Parameters
-
string
$templateFile Filename of the template.
-
array
$dataForView Data to include in rendered view.
Returns
string
_getElementFileName() ¶ protected
_getElementFileName(string $name, bool $pluginCheck = true): string|false
Finds an element filename, returns false on failure.
Parameters
-
string
$name The name of the element to find.
-
bool
$pluginCheck optional - if false will ignore the request's plugin if parsed plugin is not loaded
Returns
string|false
_getLayoutFileName() ¶ protected
_getLayoutFileName(string|null $name = null): string
Returns layout filename for this template as a string.
Parameters
-
string|null
$name optional The name of the layout to find.
Returns
string
Throws
Cake\View\Exception\MissingLayoutException
when a layout cannot be located
Cake\Core\Exception\CakeException
_getSubPaths() ¶ protected
_getSubPaths(string $basePath): list<string>
Find all sub templates path, based on $basePath If a prefix is defined in the current request, this method will prepend the prefixed template path to the $basePath, cascading up in case the prefix is nested. This is essentially used to find prefixed template paths for elements and layouts.
Parameters
-
string
$basePath Base path on which to get the prefixed one.
Returns
list<string>
_getTemplateFileName() ¶ protected
_getTemplateFileName(string|null $name = null): string
Returns filename of given action's template file as a string. CamelCased action names will be under_scored by default. This means that you can have LongActionNames that refer to long_action_names.php templates. You can change the inflection rule by overriding _inflectTemplateFileName.
Parameters
-
string|null
$name optional Controller action to find template filename for
Returns
string
Throws
Cake\View\Exception\MissingTemplateException
when a template file could not be found.
Cake\Core\Exception\CakeException
When template name not provided.
_inflectTemplateFileName() ¶ protected
_inflectTemplateFileName(string $name): string
Change the name of a view template file into underscored format.
Parameters
-
string
$name Name of file which should be inflected.
Returns
string
_paths() ¶ protected
_paths(string|null $plugin = null, bool $cached = true): list<string>
Return all possible paths to find view files in order
Parameters
-
string|null
$plugin optional Optional plugin name to scan for view files.
-
bool
$cached optional Set to false to force a refresh of view paths. Default true.
Returns
list<string>
_render() ¶ protected
_render(string $templateFile, array $data = []): string
Renders and returns output for given template filename with its array of data. Handles parent/extended templates.
Parameters
-
string
$templateFile Filename of the template
-
array
$data optional Data to include in rendered view. If empty the current View::$viewVars will be used.
Returns
string
Throws
LogicException
When a block is left open.
_renderElement() ¶ protected
_renderElement(string $file, array $data, array<string, mixed> $options): string
Renders an element and fires the before and afterRender callbacks for it and writes to the cache if a cache is used
Parameters
-
string
$file Element file path
-
array
$data Data to render
-
array<string, mixed>
$options Element options
Returns
string
addHelper() ¶ protected
addHelper(string $helper, array<string, mixed> $config = []): void
Adds a helper from within initialize()
method.
Parameters
-
string
$helper Helper.
-
array<string, mixed>
$config optional Config.
Returns
void
append() ¶ public
append(string $name, mixed $value = null): $this
Append to an existing or new block.
Appending to a new block will create the block.
Parameters
-
string
$name Name of the block
-
mixed
$value optional The content for the block. Value will be type cast to string.
Returns
$this
See Also
assign() ¶ public
assign(string $name, mixed $value): $this
Set the content for a block. This will overwrite any existing content.
Parameters
-
string
$name Name of the block
-
mixed
$value The content for the block. Value will be type cast to string.
Returns
$this
See Also
blocks() ¶ public
blocks(): list<string>
Get the names of all the existing blocks.
Returns
list<string>
See Also
cache() ¶ public
cache(callable $block, array<string, mixed> $options = []): string
Create a cached block of view logic.
This allows you to cache a block of view output into the cache
defined in elementCache
.
This method will attempt to read the cache first. If the cache is empty, the $block will be run and the output stored.
Parameters
-
callable
$block The block of code that you want to cache the output of.
-
array<string, mixed>
$options optional The options defining the cache key etc.
Returns
string
Throws
InvalidArgumentException
When $options is lacking a 'key' option.
cell() ¶ protected
cell(string $cell, array $data = [], array<string, mixed> $options = []): Cake\View\Cell
Renders the given cell.
Example:
// Taxonomy\View\Cell\TagCloudCell::smallList()
$cell = $this->cell('Taxonomy.TagCloud::smallList', ['limit' => 10]);
// App\View\Cell\TagCloudCell::smallList()
$cell = $this->cell('TagCloud::smallList', ['limit' => 10]);
The display
action will be used by default when no action is provided:
// Taxonomy\View\Cell\TagCloudCell::display()
$cell = $this->cell('Taxonomy.TagCloud');
Cells are not rendered until they are echoed.
Parameters
-
string
$cell You must indicate cell name, and optionally a cell action. e.g.:
TagCloud::smallList
will invokeView\Cell\TagCloudCell::smallList()
,display
action will be invoked by default when none is provided.-
array
$data optional Additional arguments for cell method. e.g.:
cell('TagCloud::smallList', ['a1' => 'v1', 'a2' => 'v2'])
maps toView\Cell\TagCloud::smallList(v1, v2)
-
array<string, mixed>
$options optional Options for Cell's constructor
Returns
Cake\View\Cell
Throws
Cake\View\Exception\MissingCellException
If Cell class was not found.
configShallow() ¶ public
configShallow(array<string, mixed>|string $key, mixed|null $value = null): $this
Merge provided config with existing config. Unlike config()
which does
a recursive merge for nested keys, this method does a simple merge.
Setting a specific value:
$this->configShallow('key', $value);
Setting a nested value:
$this->configShallow('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->configShallow(['one' => 'value', 'another' => 'value']);
Parameters
-
array<string, mixed>|string
$key The key to set, or a complete array of configs.
-
mixed|null
$value optional The value to set.
Returns
$this
contentType() ¶ public static
contentType(): string
Mime-type this view class renders as.
Returns
string
disableAutoLayout() ¶ public
disableAutoLayout(): $this
Turns off CakePHP's conventional mode of applying layout files. Layouts will not be automatically applied to rendered views.
Returns
$this
dispatchEvent() ¶ public
dispatchEvent(string $name, array $data = [], TSubject|null $subject = null): Cake\Event\EventInterface<TSubject>
Wrapper for creating and dispatching events.
Returns a dispatched event.
Parameters
-
string
$name Name of the event.
-
array
$data optional Any value you wish to be transported with this event to it can be read by listeners.
-
TSubject|null
$subject optional The object that this event applies to ($this by default).
Returns
Cake\Event\EventInterface<TSubject>
element() ¶ public
element(string $name, array $data = [], array<string, mixed> $options = []): string
Renders a piece of PHP with provided parameters and returns HTML, XML, or any other string.
This realizes the concept of Elements, (or "partial layouts") and the $params array is used to send
data to be used in the element. Elements can be cached improving performance by using the cache
option.
Parameters
-
string
$name Name of template file in the
templates/element/
folder, orMyPlugin.template
to use the template element from MyPlugin. If the element is not found in the plugin, the normal view path cascade will be searched.-
array
$data optional Array of data to be made available to the rendered view (i.e. the Element)
-
array<string, mixed>
$options optional Array of options. Possible keys are:
Returns
string
Throws
Cake\View\Exception\MissingElementException
When an element is missing and `ignoreMissing` is false.
elementExists() ¶ public
elementExists(string $name): bool
Checks if an element exists
Parameters
-
string
$name Name of template file in the
templates/element/
folder, orMyPlugin.template
to check the template element from MyPlugin. If the element is not found in the plugin, the normal view path cascade will be searched.
Returns
bool
enableAutoLayout() ¶ public
enableAutoLayout(bool $enable = true): $this
Turns on or off CakePHP's conventional mode of applying layout files. On by default. Setting to off means that layouts will not be automatically applied to rendered views.
Parameters
-
bool
$enable optional Boolean to turn on/off.
Returns
$this
end() ¶ public
end(): $this
End a capturing block. The compliment to View::start()
Returns
$this
See Also
exists() ¶ public
exists(string $name): bool
Check if a block exists
Parameters
-
string
$name Name of the block
Returns
bool
extend() ¶ public
extend(string $name): $this
Provides template or element extension/inheritance. Templates can extends a parent template and populate blocks in the parent template.
Parameters
-
string
$name The template or element to 'extend' the current one with.
Returns
$this
Throws
LogicException
when you extend a template with itself or make extend loops.
LogicException
when you extend an element which doesn't exist
fetch() ¶ public
fetch(string $name, string $default = ''): string
Fetch the content for a block. If a block is empty or undefined '' will be returned.
Parameters
-
string
$name Name of the block
-
string
$default optional Default text
Returns
string
See Also
get() ¶ public
get(string $var, mixed $default = null): mixed
Returns the contents of the given View variable.
Parameters
-
string
$var The view var you want the contents of.
-
mixed
$default optional The default/fallback content of $var.
Returns
mixed
getConfig() ¶ public
getConfig(string|null $key = null, mixed $default = null): mixed
Returns the config.
Usage
Reading the whole config:
$this->getConfig();
Reading a specific value:
$this->getConfig('key');
Reading a nested value:
$this->getConfig('some.nested.key');
Reading with default value:
$this->getConfig('some-key', 'default-value');
Parameters
-
string|null
$key optional The key to get or null for the whole config.
-
mixed
$default optional The return value when the key does not exist.
Returns
mixed
getConfigOrFail() ¶ public
getConfigOrFail(string $key): mixed
Returns the config for this specific key.
The config value for this key must exist, it can never be null.
Parameters
-
string
$key The key to get.
Returns
mixed
Throws
InvalidArgumentException
getCurrentType() ¶ public
getCurrentType(): string
Retrieve the current template type
Returns
string
getElementPaths() ¶ protected
getElementPaths(string|null $plugin): Generator
Get an iterator for element paths.
Parameters
-
string|null
$plugin The plugin to fetch paths for.
Returns
Generator
getEventManager() ¶ public
getEventManager(): Cake\Event\EventManagerInterface
Returns the Cake\Event\EventManager manager instance for this object.
You can use this instance to register any new listeners or callbacks to the object events, or create your own events and trigger them at will.
Returns
Cake\Event\EventManagerInterface
getLayout() ¶ public
getLayout(): string
Get the name of the layout file to render the template inside of.
The name specified is the filename of the layout in templates/layout/
without the .php extension.
Returns
string
getLayoutPaths() ¶ protected
getLayoutPaths(string|null $plugin): Generator
Get an iterator for layout paths.
Parameters
-
string|null
$plugin The plugin to fetch paths for.
Returns
Generator
getRequest() ¶ public
getRequest(): Cake\Http\ServerRequest
Gets the request instance.
Returns
Cake\Http\ServerRequest
getResponse() ¶ public
getResponse(): Cake\Http\Response
Gets the response instance.
Returns
Cake\Http\Response
getSubDir() ¶ public
getSubDir(): string
Get sub-directory for this template files.
Returns
string
See Also
getTemplate() ¶ public
getTemplate(): string
Get the name of the template file to render. The name specified is the
filename in templates/<SubFolder>/
without the .php extension.
Returns
string
getVars() ¶ public
getVars(): list<string>
Returns a list of variables available in the current View context
Returns
list<string>
helpers() ¶ public
helpers(): Cake\View\HelperRegistry
Get the helper registry in use by this View class.
Returns
Cake\View\HelperRegistry
initialize() ¶ public
initialize(): void
Initialization hook method.
Properties like $helpers etc. cannot be initialized statically in your custom view class as they are overwritten by values from controller in constructor. So this method allows you to manipulate them as required after view instance is constructed.
Helpers can be added using {@link addHelper()} method.
Returns
void
isAutoLayoutEnabled() ¶ public
isAutoLayoutEnabled(): bool
Returns if CakePHP's conventional mode of applying layout files is enabled. Disabled means that layouts will not be automatically applied to rendered views.
Returns
bool
loadHelper() ¶ public
loadHelper(string $name, array<string, mixed> $config = []): Cake\View\Helper
Loads a helper. Delegates to the HelperRegistry::load()
to load the helper.
You should use addHelper()
instead of this method from the initialize()
hook of AppView
or other custom View classes.
Parameters
-
string
$name Name of the helper to load.
-
array<string, mixed>
$config optional Settings for the helper
Returns
Cake\View\Helper
See Also
loadHelpers() ¶ public
loadHelpers(): $this
Interact with the HelperRegistry to load all the helpers.
Returns
$this
log() ¶ public
log(Stringable|string $message, string|int $level = LogLevel::ERROR, array|string $context = []): bool
Convenience method to write a message to Log. See Log::write() for more information on writing to logs.
Parameters
-
Stringable|string
$message Log message.
-
string|int
$level optional Error level.
-
array|string
$context optional Additional log data relevant to this message.
Returns
bool
pluginSplit() ¶ public
pluginSplit(string $name, bool $fallback = true): array
Splits a dot syntax plugin name into its plugin and filename. If $name does not have a dot, then index 0 will be null. It checks if the plugin is loaded, else filename will stay unchanged for filenames containing dot
Parameters
-
string
$name The name you want to plugin split.
-
bool
$fallback optional If true uses the plugin set in the current Request when parsed plugin is not loaded
Returns
array
prepend() ¶ public
prepend(string $name, mixed $value): $this
Prepend to an existing or new block.
Prepending to a new block will create the block.
Parameters
-
string
$name Name of the block
-
mixed
$value The content for the block. Value will be type cast to string.
Returns
$this
See Also
render() ¶ public
render(string|null $template = null, string|false|null $layout = null): string
Renders view for given template file and layout.
Render triggers helper callbacks, which are fired before and after the template are rendered, as well as before and after the layout. The helper callbacks are called:
beforeRender
afterRender
beforeLayout
afterLayout
If View::$autoLayout is set to false
, the template will be returned bare.
Template and layout names can point to plugin templates or layouts. Using the Plugin.template
syntax
a plugin template/layout/ can be used instead of the app ones. If the chosen plugin is not found
the template will be located along the regular view path cascade.
Parameters
-
string|null
$template optional Name of template file to use
-
string|false|null
$layout optional Layout to use. False to disable.
Returns
string
Throws
Cake\Core\Exception\CakeException
If there is an error in the view.
renderLayout() ¶ public
renderLayout(string $content, string|null $layout = null): string
Renders a layout. Returns output from _render().
Several variables are created for use in layout.
Parameters
-
string
$content Content to render in a template, wrapped by the surrounding layout.
-
string|null
$layout optional Layout name
Returns
string
Throws
Cake\Core\Exception\CakeException
if there is an error in the view.
reset() ¶ public
reset(string $name): $this
Reset the content for a block. This will overwrite any existing content.
Parameters
-
string
$name Name of the block
Returns
$this
See Also
set() ¶ public
set(array|string $name, mixed $value = null): $this
Saves a variable or an associative array of variables for use inside a template.
Parameters
-
array|string
$name A string or an array of data.
-
mixed
$value optional Value in case $name is a string (which then works as the key). Unused if $name is an associative array, otherwise serves as the values to $name's keys.
Returns
$this
Throws
Cake\Core\Exception\CakeException
If the array combine operation failed.
setConfig() ¶ public
setConfig(array<string, mixed>|string $key, mixed|null $value = null, bool $merge = true): $this
Sets the config.
Usage
Setting a specific value:
$this->setConfig('key', $value);
Setting a nested value:
$this->setConfig('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->setConfig(['one' => 'value', 'another' => 'value']);
Parameters
-
array<string, mixed>|string
$key The key to set, or a complete array of configs.
-
mixed|null
$value optional The value to set.
-
bool
$merge optional Whether to recursively merge or overwrite existing config, defaults to true.
Returns
$this
Throws
Cake\Core\Exception\CakeException
When trying to set a key that is invalid.
setContentType() ¶ protected
setContentType(): void
Set the response content-type based on the view's contentType()
Returns
void
setElementCache() ¶ public
setElementCache(string $elementCache): $this
Set The cache configuration View will use to store cached elements
Parameters
-
string
$elementCache Cache config name.
Returns
$this
See Also
setEventManager() ¶ public
setEventManager(Cake\Event\EventManagerInterface $eventManager): $this
Returns the Cake\Event\EventManagerInterface instance for this object.
You can use this instance to register any new listeners or callbacks to the object events, or create your own events and trigger them at will.
Parameters
-
Cake\Event\EventManagerInterface
$eventManager the eventManager to set
Returns
$this
setLayout() ¶ public
setLayout(string $name): $this
Set the name of the layout file to render the template inside of.
The name specified is the filename of the layout in templates/layout/
without the .php extension.
Parameters
-
string
$name Layout file name to set.
Returns
$this
setLayoutPath() ¶ public
setLayoutPath(string $path): $this
Set path for layout files.
Parameters
-
string
$path Path for layout files.
Returns
$this
setPlugin() ¶ public
setPlugin(string|null $name): $this
Sets the plugin name.
Parameters
-
string|null
$name Plugin name.
Returns
$this
setRequest() ¶ public
setRequest(Cake\Http\ServerRequest $request): $this
Sets the request objects and configures a number of controller properties based on the contents of the request. The properties that get set are:
- $this->request - To the $request parameter
- $this->plugin - To the value returned by $request->getParam('plugin')
Parameters
-
Cake\Http\ServerRequest
$request Request instance.
Returns
$this
setResponse() ¶ public
setResponse(Cake\Http\Response $response): $this
Sets the response instance.
Parameters
-
Cake\Http\Response
$response Response instance.
Returns
$this
setSubDir() ¶ public
setSubDir(string $subDir): $this
Set sub-directory for this template files.
Parameters
-
string
$subDir Sub-directory name.
Returns
$this
See Also
setTemplate() ¶ public
setTemplate(string $name): $this
Set the name of the template file to render. The name specified is the
filename in templates/<SubFolder>/
without the .php extension.
Parameters
-
string
$name Template file name to set.
Returns
$this
setTemplatePath() ¶ public
setTemplatePath(string $path): $this
Set path for templates files.
Parameters
-
string
$path Path for template files.
Returns
$this
setTheme() ¶ public
setTheme(string|null $theme): $this
Set the view theme to use.
Parameters
-
string|null
$theme Theme name.
Returns
$this
start() ¶ public
start(string $name): $this
Start capturing output for a 'block'
You can use start on a block multiple times to append or prepend content in a capture mode.
// Append content to an existing block.
$this->start('content');
echo $this->fetch('content');
echo 'Some new content';
$this->end();
// Prepend content to an existing block
$this->start('content');
echo 'Some new content';
echo $this->fetch('content');
$this->end();
Parameters
-
string
$name The name of the block to capture for.
Returns
$this
See Also
Property Detail
$_configInitialized ¶ protected
Whether the config property has already been configured with defaults
Type
bool
$_current ¶ protected
The currently rendering view file. Used for resolving parent files.
Type
string
$_currentType ¶ protected
Currently rendering an element. Used for finding parent fragments for elements.
Type
string
$_eventManager ¶ protected
Instance of the Cake\Event\EventManager this object is using to dispatch inner events.
Type
Cake\Event\EventManagerInterface|null
$_parents ¶ protected
The names of views and their parents used with View::extend();
Type
array<string, string>
$_passedVars ¶ protected
List of variables to collect from the associated controller.
Type
list<string>
$_stack ¶ protected
Content stack, used for nested templates that all use View::extend();
Type
list<string>
$autoLayout ¶ protected
Turns on or off CakePHP's conventional mode of applying layout files. On by default. Setting to off means that layouts will not be automatically applied to rendered templates.
Type
bool
$elementCache ¶ protected
The Cache configuration View will use to store cached elements. Changing this will change the default configuration elements are stored under. You can also choose a cache config per element.
Type
string
$helpers ¶ protected
A configuration array for helpers to be loaded.
Type
array<string, array<string, mixed>>
$layout ¶ protected
The name of the layout file to render the template inside of. The name specified
is the filename of the layout in templates/layout/
without the .php
extension.
Type
string
$layoutPath ¶ protected
The name of the layouts subfolder containing layouts for this View.
Type
string
$request ¶ protected
An instance of a \Cake\Http\ServerRequest object that contains information about the current request. This object contains all the information about a request and several methods for reading additional information about the request.
Type
Cake\Http\ServerRequest
$subDir ¶ protected
Sub-directory for this template file. This is often used for extension based routing.
Eg. With an xml
extension, $subDir would be xml/
Type
string
$template ¶ protected
The name of the template file to render. The name specified
is the filename in templates/<SubFolder>/
without the .php extension.
Type
string