Fr3nch13/CakePHP Utilities

ViewBuilder
in package
implements JsonSerializable, Serializable

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

Table of Contents

Interfaces

JsonSerializable
Serializable

Properties

$_autoLayout  : bool
Whether autoLayout should be enabled.
$_className  : string|null
The view class name to use.
$_helpers  : array<string|int, mixed>
The helpers to use
$_layout  : string|null
The layout name to render.
$_layoutPath  : string|null
The layout path to build the view with.
$_name  : string|null
The view variables to use
$_options  : array<string, mixed>
Additional options used when constructing the view.
$_plugin  : string|null
The plugin name to use.
$_template  : string|null
The template file to render.
$_templatePath  : string|null
The subdirectory to the template.
$_theme  : string|null
The theme name to use.
$_vars  : array<string, mixed>
View vars

Methods

__serialize()  : array<string|int, mixed>
Magic method used for serializing the view builder object.
__unserialize()  : void
Magic method used to rebuild the view builder object.
addHelper()  : $this
Adds a helper to use.
addHelpers()  : $this
Adds helpers to use by merging with existing ones.
build()  : View
Using the data in the builder, create a view instance.
createFromArray()  : $this
Configures a view builder instance from serialized config.
disableAutoLayout()  : $this
Turns off CakePHP's conventional mode of applying layout files.
enableAutoLayout()  : $this
Turns on or off CakePHP's conventional mode of applying layout files.
getClassName()  : string|null
Gets the view classname.
getHelpers()  : array<string|int, mixed>
Gets the helpers to use.
getLayout()  : string|null
Gets the name of the layout file to render the view inside of.
getLayoutPath()  : string|null
Gets path for layout files.
getName()  : string|null
Gets the view name.
getOption()  : mixed
Get view option.
getOptions()  : array<string, mixed>
Gets additional options for the view.
getPlugin()  : string|null
Gets the plugin name to use.
getTemplate()  : string|null
Gets the name of the view file to render. The name specified is the filename in `templates/<SubFolder>/` without the .php extension.
getTemplatePath()  : string|null
Gets path for template files.
getTheme()  : string|null
Gets the view theme to use.
getVar()  : mixed
Get view var
getVars()  : array<string, mixed>
Get all view vars.
hasVar()  : bool
Check if view var is set.
isAutoLayoutEnabled()  : bool
Returns if CakePHP's conventional mode of applying layout files is enabled.
jsonSerialize()  : array<string|int, mixed>
Serializes the view builder object to a value that can be natively serialized and re-used to clone this builder instance.
serialize()  : string
Serializes the view builder object.
setClassName()  : $this
Sets the view classname.
setHelpers()  : $this
Sets the helpers to use.
setLayout()  : $this
Sets the name of the layout file to render the view inside of.
setLayoutPath()  : $this
Sets path for layout files.
setName()  : $this
Sets the view name.
setOption()  : $this
Set view option.
setOptions()  : $this
Sets additional options for the view.
setPlugin()  : $this
Sets the plugin name to use.
setTemplate()  : $this
Sets the name of the view file to render. The name specified is the filename in `templates/<SubFolder>/` without the .php extension.
setTemplatePath()  : $this
Sets path for template files.
setTheme()  : $this
Sets the view theme to use.
setVar()  : $this
Saves a variable for use inside a template.
setVars()  : $this
Saves view vars for use inside templates.
unserialize()  : void
Unserializes the view builder object.
_checkViewVars()  : void
Iterates through hash to clean up and normalize.

Properties

$_autoLayout

Whether autoLayout should be enabled.

protected bool $_autoLayout = true

$_className

The view class name to use.

protected string|null $_className

Can either use plugin notation, a short name or a fully namespaced classname.

Tags
psalm-var

class-string<\Cake\View\View>|string|null

$_helpers

The helpers to use

protected array<string|int, mixed> $_helpers = []

$_layout

The layout name to render.

protected string|null $_layout

$_layoutPath

The layout path to build the view with.

protected string|null $_layoutPath

$_name

The view variables to use

protected string|null $_name

$_options

Additional options used when constructing the view.

protected array<string, mixed> $_options = []

These options array lets you provide custom constructor arguments to application/plugin view classes.

$_plugin

The plugin name to use.

protected string|null $_plugin

$_template

The template file to render.

protected string|null $_template

$_templatePath

The subdirectory to the template.

protected string|null $_templatePath

$_theme

The theme name to use.

protected string|null $_theme

$_vars

View vars

protected array<string, mixed> $_vars = []

Methods

__serialize()

Magic method used for serializing the view builder object.

public __serialize() : array<string|int, mixed>
Return values
array<string|int, mixed>

__unserialize()

Magic method used to rebuild the view builder object.

public __unserialize(array<string, mixed> $data) : void
Parameters
$data : array<string, mixed>

Data array.

addHelper()

Adds a helper to use.

public addHelper(string $helper[, array<string, mixed> $options = [] ]) : $this
Parameters
$helper : string

Helper to use.

$options : array<string, mixed> = []

Options.

Tags
since
4.1.0
Return values
$this

addHelpers()

Adds helpers to use by merging with existing ones.

public addHelpers(array<string|int, mixed> $helpers) : $this
Parameters
$helpers : array<string|int, mixed>

Helpers to use.

Tags
since
4.3.0
Return values
$this

build()

Using the data in the builder, create a view instance.

public build([array<string, mixed> $vars = [] ][, ServerRequest|null $request = null ][, Response|null $response = null ][, EventManagerInterface|null $events = null ]) : View

If className() is null, App\View\AppView will be used. If that class does not exist, then will be used.

Parameters
$vars : array<string, mixed> = []

The view variables/context to use.

$request : ServerRequest|null = null

The request to use.

$response : Response|null = null

The response to use.

$events : EventManagerInterface|null = null

The event manager to use.

Tags
throws
MissingViewException
Return values
View

createFromArray()

Configures a view builder instance from serialized config.

public createFromArray(array<string, mixed> $config) : $this
Parameters
$config : array<string, mixed>

View builder configuration array.

Return values
$this

disableAutoLayout()

Turns off CakePHP's conventional mode of applying layout files.

public disableAutoLayout() : $this

Setting to off means that layouts will not be automatically applied to rendered views.

Return values
$this

enableAutoLayout()

Turns on or off CakePHP's conventional mode of applying layout files.

public enableAutoLayout([bool $enable = true ]) : $this

On by default. Setting to off means that layouts will not be automatically applied to rendered views.

Parameters
$enable : bool = true

Boolean to turn on/off.

Return values
$this

getClassName()

Gets the view classname.

public getClassName() : string|null
Return values
string|null

getHelpers()

Gets the helpers to use.

public getHelpers() : array<string|int, mixed>
Return values
array<string|int, mixed>

getLayout()

Gets the name of the layout file to render the view inside of.

public getLayout() : string|null
Return values
string|null

getLayoutPath()

Gets path for layout files.

public getLayoutPath() : string|null
Return values
string|null

getName()

Gets the view name.

public getName() : string|null
Return values
string|null

getOption()

Get view option.

public getOption(string $name) : mixed
Parameters
$name : string

The name of the option.

getOptions()

Gets additional options for the view.

public getOptions() : array<string, mixed>
Return values
array<string, mixed>

getPlugin()

Gets the plugin name to use.

public getPlugin() : string|null
Return values
string|null

getTemplate()

Gets the name of the view file to render. The name specified is the filename in `templates/<SubFolder>/` without the .php extension.

public getTemplate() : string|null
Return values
string|null

getTemplatePath()

Gets path for template files.

public getTemplatePath() : string|null
Return values
string|null

getTheme()

Gets the view theme to use.

public getTheme() : string|null
Return values
string|null

getVar()

Get view var

public getVar(string $name) : mixed
Parameters
$name : string

Var name

Return values
mixed

The var value or null if unset.

getVars()

Get all view vars.

public getVars() : array<string, mixed>
Return values
array<string, mixed>

hasVar()

Check if view var is set.

public hasVar(string $name) : bool
Parameters
$name : string

Var name

Return values
bool

isAutoLayoutEnabled()

Returns if CakePHP's conventional mode of applying layout files is enabled.

public isAutoLayoutEnabled() : bool

Disabled means that layouts will not be automatically applied to rendered views.

Return values
bool

jsonSerialize()

Serializes the view builder object to a value that can be natively serialized and re-used to clone this builder instance.

public jsonSerialize() : array<string|int, mixed>

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.
Return values
array<string|int, mixed>

Serializable array of configuration properties.

serialize()

Serializes the view builder object.

public serialize() : string
Return values
string

setClassName()

Sets the view classname.

public setClassName(string|null $name) : $this

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
$name : string|null

The class name for the view.

Return values
$this

setHelpers()

Sets the helpers to use.

public setHelpers(array<string|int, mixed> $helpers[, bool $merge = true ]) : $this
Parameters
$helpers : array<string|int, mixed>

Helpers to use.

$merge : bool = true

Whether to merge existing data with the new data.

Return values
$this

setLayout()

Sets the name of the layout file to render the view inside of.

public setLayout(string|null $name) : $this

The name specified is the filename of the layout in templates/layout/ without the .php extension.

Parameters
$name : string|null

Layout file name to set.

Return values
$this

setLayoutPath()

Sets path for layout files.

public setLayoutPath(string|null $path) : $this
Parameters
$path : string|null

Path for layout files.

Return values
$this

setName()

Sets the view name.

public setName(string|null $name) : $this
Parameters
$name : string|null

The name of the view, or null to remove the current name.

Return values
$this

setOption()

Set view option.

public setOption(string $name, mixed $value) : $this
Parameters
$name : string

The name of the option.

$value : mixed

Value to set.

Return values
$this

setOptions()

Sets additional options for the view.

public setOptions(array<string, mixed> $options[, bool $merge = true ]) : $this

This lets you provide custom constructor arguments to application/plugin view classes.

Parameters
$options : array<string, mixed>

An array of options.

$merge : bool = true

Whether to merge existing data with the new data.

Return values
$this

setPlugin()

Sets the plugin name to use.

public setPlugin(string|null $name) : $this
Parameters
$name : string|null

Plugin name. Use null to remove the current plugin name.

Return values
$this

setTemplate()

Sets the name of the view file to render. The name specified is the filename in `templates/<SubFolder>/` without the .php extension.

public setTemplate(string|null $name) : $this
Parameters
$name : string|null

View file name to set, or null to remove the template name.

Return values
$this

setTemplatePath()

Sets path for template files.

public setTemplatePath(string|null $path) : $this
Parameters
$path : string|null

Path for view files.

Return values
$this

setTheme()

Sets the view theme to use.

public setTheme(string|null $theme) : $this
Parameters
$theme : string|null

Theme name. Use null to remove the current theme.

Return values
$this

setVar()

Saves a variable for use inside a template.

public setVar(string $name[, mixed $value = null ]) : $this
Parameters
$name : string

A string or an array of data.

$value : mixed = null

Value.

Return values
$this

setVars()

Saves view vars for use inside templates.

public setVars(array<string, mixed> $data[, bool $merge = true ]) : $this
Parameters
$data : array<string, mixed>

Array of data.

$merge : bool = true

Whether to merge with existing vars, default true.

Return values
$this

unserialize()

Unserializes the view builder object.

public unserialize(string $data) : void
Parameters
$data : string

Serialized string.

_checkViewVars()

Iterates through hash to clean up and normalize.

protected _checkViewVars(mixed &$item, string $key) : void
Parameters
$item : mixed

Reference to the view var value.

$key : string

View var key.

Tags
throws
RuntimeException

        
On this page

Search results