Class ViewBuilder
Provides an API for iteratively building a view up.
Once you have configured the view and established all the context you can create a view instance with build().
Properties summary
- $_autoLayout protected
boolWhether or not autoLayout should be enabled.
- $_className protected
string|nullThe view class name to use.
- $_helpers protected
arrayThe helpers to use
- $_layout protected
string|nullThe layout name to render.
- $_layoutPath protected
string|nullThe layout path to build the view with.
- $_name protected
string|nullThe view variables to use
- $_options protected
arrayAdditional options used when constructing the view.
- $_plugin protected
string|nullThe plugin name to use.
- $_template protected
string|nullThe template file to render.
- $_templatePath protected
string|nullThe subdirectory to the template.
- $_theme protected
string|nullThe theme name to use.
- $_vars protected
arrayView vars
Method Summary
- getTemplate() public
Gets the name of the view file to render. The name specified is the filename in
templates/<SubFolder>/without the .php extension. - isAutoLayoutEnabled() public
Returns if CakePHP's conventional mode of applying layout files is enabled.
- jsonSerialize() public
Serializes the view builder object to a value that can be natively serialized and re-used to clone this builder instance.
- setTemplate() public
Sets the name of the view file to render. The name specified is the filename in
templates/<SubFolder>/without the .php extension.
Method Detail
_checkViewVars() protected
_checkViewVars(mixed $item, string $key)
Iterates through hash to clean up and normalize.
Parameters
-
mixed$item Reference to the view var value.
-
string$key View var key.
Throws
RuntimeExceptionaddHelper() public
addHelper(string $helper)
Adds a helper to use.
Parameters
-
string$helper Helper to use.
Returns
$thisbuild() public
build(array $vars, ?\Cake\Http\ServerRequest $request, ?\Cake\Http\Response $response, ?\Cake\Event\EventManagerInterface $events)
Using the data in the builder, create a view instance.
If className() is null, App\View\AppView will be used. If that class does not exist, then Cake\View\View will be used.
Parameters
-
array$vars optional The view variables/context to use.
-
\Cake\Http\ServerRequest|null$request optional The request to use.
-
\Cake\Http\Response|null$response optional The response to use.
-
\Cake\Event\EventManagerInterface|null$events optional The event manager to use.
Returns
\Cake\View\ViewThrows
Cake\View\Exception\MissingViewExceptioncreateFromArray() public
createFromArray(array $config)
Configures a view builder instance from serialized config.
Parameters
-
array$config View builder configuration array.
Returns
$thisConfigured view builder instance.
disableAutoLayout() public
disableAutoLayout()
Turns off CakePHP's conventional mode of applying layout files.
Setting to off means that layouts will not be automatically applied to rendered views.
Returns
$thisenableAutoLayout() public
enableAutoLayout(bool $enable)
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
$thisgetClassName() public
getClassName()
Gets the view classname.
Returns
string|nullgetHelpers() public
getHelpers()
Gets the helpers to use.
Returns
arraygetLayout() public
getLayout()
Gets the name of the layout file to render the view inside of.
Returns
string|nullgetLayoutPath() public
getLayoutPath()
Gets path for layout files.
Returns
string|nullgetName() public
getName()
Gets the view name.
Returns
string|nullgetOption() public
getOption(string $name)
Get view option.
Parameters
-
string$name The name of the option.
Returns
mixedgetOptions() public
getOptions()
Gets additional options for the view.
Returns
arraygetPlugin() public
getPlugin()
Gets the plugin name to use.
Returns
string|nullgetTemplate() public
getTemplate()
Gets the name of the view file to render. The name specified is the filename in templates/<SubFolder>/ without the .php extension.
Returns
string|nullgetTemplatePath() public
getTemplatePath()
Gets path for template files.
Returns
string|nullgetTheme() public
getTheme()
Gets the view theme to use.
Returns
string|nullgetVar() public
getVar(string $name)
Get view var
Parameters
-
string$name Var name
Returns
mixedThe var value or null if unset.
getVars() public
getVars()
Get all view vars.
Returns
arrayhasVar() public
hasVar(string $name)
Check if view var is set.
Parameters
-
string$name Var name
Returns
boolisAutoLayoutEnabled() public
isAutoLayoutEnabled()
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
booljsonSerialize() public
jsonSerialize()
Serializes the view builder object to a value that can be natively serialized and re-used to clone this builder instance.
There are limitations for viewVars that are good to know:
- ORM\Query executed and stored as resultset
- SimpleXMLElements stored as associative array
- Exceptions stored as strings
- Resources, \Closure and \PDO are not supported.
Returns
arraySerializable array of configuration properties.
serialize() public
serialize()
Serializes the view builder object.
Returns
stringsetClassName() public
setClassName(?string $name)
Sets the view classname.
Accepts either a short name (Ajax) a plugin name (MyPlugin.Ajax) or a fully namespaced name (App\View\AppView) or null to use the View class provided by CakePHP.
Parameters
-
string|null$name The class name for the view.
Returns
$thissetHelpers() public
setHelpers(array $helpers, bool $merge)
Sets the helpers to use.
Parameters
-
array$helpers Helpers to use.
-
bool$merge optional Whether or not to merge existing data with the new data.
Returns
$thissetLayout() public
setLayout(?string $name)
Sets the name of the layout file to render the view inside of.
The name specified is the filename of the layout in templates/layout/ without the .php extension.
Parameters
-
string|null$name Layout file name to set.
Returns
$thissetLayoutPath() public
setLayoutPath(?string $path)
Sets path for layout files.
Parameters
-
string|null$path Path for layout files.
Returns
$thissetName() public
setName(?string $name)
Sets the view name.
Parameters
-
string|null$name The name of the view, or null to remove the current name.
Returns
$thissetOption() public
setOption(string $name, mixed $value)
Set view option.
Parameters
-
string$name The name of the option.
-
mixed$value Value to set.
Returns
$thissetOptions() public
setOptions(array $options, bool $merge)
Sets additional options for the view.
This lets you provide custom constructor arguments to application/plugin view classes.
Parameters
-
array$options An array of options.
-
bool$merge optional Whether or not to merge existing data with the new data.
Returns
$thissetPlugin() public
setPlugin(?string $name)
Sets the plugin name to use.
Parameters
-
string|null$name Plugin name. Use null to remove the current plugin name.
Returns
$thissetTemplate() public
setTemplate(?string $name)
Sets the name of the view file to render. The name specified is the filename in templates/<SubFolder>/ without the .php extension.
Parameters
-
string|null$name View file name to set, or null to remove the template name.
Returns
$thissetTemplatePath() public
setTemplatePath(?string $path)
Sets path for template files.
Parameters
-
string|null$path Path for view files.
Returns
$thissetTheme() public
setTheme(?string $theme)
Sets the view theme to use.
Parameters
-
string|null$theme Theme name. Use null to remove the current theme.
Returns
$thissetVar() public
setVar(string $name, mixed $value)
Saves a variable for use inside a template.
Parameters
-
string$name A string or an array of data.
-
mixed$value optional Value.
Returns
$thissetVars() public
setVars(array $data, bool $merge)
Saves view vars for use inside templates.
Parameters
-
array$data Array of data.
-
bool$merge optional Whether to merge with existing vars, default true.
Returns
$thisunserialize() public
unserialize(mixed $data)
Unserializes the view builder object.
Parameters
-
string$data Serialized string.
Property Detail
$_autoLayout protected
Whether or not autoLayout should be enabled.
Type
bool$_className protected
The view class name to use.
Can either use plugin notation, a short name or a fully namespaced classname.
Type
string|null$_helpers protected
The helpers to use
Type
array$_layout protected
The layout name to render.
Type
string|null$_layoutPath protected
The layout path to build the view with.
Type
string|null$_name protected
The view variables to use
Type
string|null$_options protected
Additional options used when constructing the view.
This options array lets you provide custom constructor arguments to application/plugin view classes.
Type
array$_plugin protected
The plugin name to use.
Type
string|null$_template protected
The template file to render.
Type
string|null$_templatePath protected
The subdirectory to the template.
Type
string|null$_theme protected
The theme name to use.
Type
string|null$_vars protected
View vars
Type
array
© 2005–present The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/4.1/class-Cake.View.ViewBuilder.html