class View

Encapsulates data for displaying a form or result of a form submission and uses the Twig template engine to render its output.

This class manages the parameters that are involved in the form (which should implement ParameterInterface) and allows for a general PHP redirect. It initializes the Twig template engine and passes the stored parameters to it. Twig is then used to render and display the form as specified in the main template. This view also passes on the $_SERVER superglobal to the template (accessible as "_server"). Exceptions generated by Twig are shown as errors and logged accordingly.

Properties

private string $templateName The name of the view (the template file that is to be rendered).
private string $templateDirectory The relative path to the directory where the template files are stored.
private string $templateCacheDirectory The relative path where cached/compiled templates are to be stored.
private array $params An array of parameters used for display.
private FilesystemLoader $loader The Twig loader instance.
private Environment $twig The main instance of the Twig template engine (environment).

Methods

__construct(string $templateName, string $templateDirectory = "templates", string $templateCacheDirectory = "templates_c", array $params = [])

Creates a new view with the main template to be displayed, the path to the template and compiled templates directory as well as parameters of the form. Also initializes the Twig template engine with caching and auto reload enabled.

string
getTemplateName()

Returns the name of the main template that's being used for display.

array
getParameters()

Returns the supplied parameters.

setParameter(ParameterInterface $param)

Allows to add or redefine parameters when the view object already exists. This avoids having to create a completely new view object just because one parameter has changed or needs to be added. This method first checks if a parameter with the given name is already stored within the view. If so, it updates its value with the one supplied in $param. If the parameter is not present in the view, it is being added.

display()

Displays the current view. Iterates over all the parameters and stores them in a temporary, associative array.

static 
redirectTo(string $location, array $queryParameters = null)

Performs a generic redirect using header(). GET-Parameters may optionally be supplied as an associative array.

Details

at line 84
__construct(string $templateName, string $templateDirectory = "templates", string $templateCacheDirectory = "templates_c", array $params = [])

Creates a new view with the main template to be displayed, the path to the template and compiled templates directory as well as parameters of the form. Also initializes the Twig template engine with caching and auto reload enabled.

Parameters

string $templateName The name of the template to be displayed.
string $templateDirectory The path where the template file is located (default is "templates").
string $templateCacheDirectory The path where cached template files are to be stored (default is "templates_c").
array $params The parameters used when displaying the view.

at line 107
string getTemplateName()

Returns the name of the main template that's being used for display.

Return Value

string The template name.

at line 116
array getParameters()

Returns the supplied parameters.

Return Value

array The parameters.

at line 128
setParameter(ParameterInterface $param)

Allows to add or redefine parameters when the view object already exists. This avoids having to create a completely new view object just because one parameter has changed or needs to be added. This method first checks if a parameter with the given name is already stored within the view. If so, it updates its value with the one supplied in $param. If the parameter is not present in the view, it is being added.

Parameters

ParameterInterface $param The parameter to be added or updated.

at line 145
display()

Displays the current view. Iterates over all the parameters and stores them in a temporary, associative array.

Twig then displays the main template, using the array with the parameters. Exceptions generated by Twig are shown as errors and logged accordingly for simplification.

at line 176
static redirectTo(string $location, array $queryParameters = null)

Performs a generic redirect using header(). GET-Parameters may optionally be supplied as an associative array.

Parameters

string $location The target location for the redirect.
array $queryParameters GET-Parameters for HTTP-Request