class ViewFactory

View factory.

Traits

Namespaced file loader trait.

Properties

protected string $path Path.
protected string $extension File extension. from NamespacedFileLoaderTrait
protected array $namespaces Namespaces. from NamespacedFileLoaderTrait
protected FileSystem $fileSystem File sytem.
protected string $charset Charset.
protected Container $container Container.
protected array $renderers View renderers.
protected array $globalVariables Global view variables.
protected array $autoAssignVariables Variables that should be auto assigned to views.
protected array $viewCache View cache.
protected array $rendererInstances Renderer instances.

Methods

setPath(string $path)

Sets the default path.

setExtension(string $extension)

Sets the extension.

registerNamespace(string $namespace, string $path)

Registers a namespace.

string
getFilePath(string $file, string|null $extension = null, string|null $suffix = null)

Returns the path to the file.

array
getCascadingFilePaths(string $file, string|null $extension = null, string|null $suffix = null)

Returns an array of cascading file paths.

__construct(FileSystem $fileSystem, string $path, string $charset = 'UTF-8', Container|null $container = null)

Constructor.

string
getCharset()

Returns the charset.

setCharset(string $charset)

Sets the charset.

extend(string $extension, Closure|string $renderer)

Registers a custom view renderer.

assign(string $name, mixed $value)

Assign a global view variable that will be available in all views.

autoAssign(string $view, callable $variables)

Assign variables that should be auto assigned to views upon creation.

clearAutoAssignVariables()

Clears the autoassign variables.

array|false
getViewPathAndExtension(string $view, bool $throwException = true)

Returns an array containing the view path and the renderer we should use.

rendererFactory(Closure|string $renderer)

Creates a renderer instance.

resolveRenderer(string $extension)

Returns a renderer instance.

bool
exists(string $view)

Returns TRUE if the view exists and FALSE if not.

array
getAutoAssignVariablesForView(string $view)

Returns view specific auto assign variables.

array
getAutoAssignVariables(string $view)

Returns auto assign variables for a view.

array
mergeVariables(string $view, array $variables)

Returns array where variables have been merged in order of importance.

create(string $view, array $variables = [])

Creates and returns a view instance.

string
render(string $view, array $variables = [])

Creates and returns a rendered view.

Details

setPath(string $path)

Sets the default path.

Parameters

string $path Path

setExtension(string $extension)

Sets the extension.

Parameters

string $extension Extension

registerNamespace(string $namespace, string $path)

Registers a namespace.

Parameters

string $namespace Namespace name
string $path Namespace path

protected string getFilePath(string $file, string|null $extension = null, string|null $suffix = null)

Returns the path to the file.

Parameters

string $file Filename
string|null $extension File extension
string|null $suffix Path suffix

Return Value

string

protected array getCascadingFilePaths(string $file, string|null $extension = null, string|null $suffix = null)

Returns an array of cascading file paths.

Parameters

string $file Filename
string|null $extension File extension
string|null $suffix Path suffix

Return Value

array

at line 97
__construct(FileSystem $fileSystem, string $path, string $charset = 'UTF-8', Container|null $container = null)

Constructor.

Parameters

FileSystem $fileSystem File system instance
string $path Default path
string $charset Charset
Container|null $container Container

at line 115
string getCharset()

Returns the charset.

Return Value

string

at line 126
ViewFactory setCharset(string $charset)

Sets the charset.

Parameters

string $charset Charset

Return Value

ViewFactory

at line 140
ViewFactory extend(string $extension, Closure|string $renderer)

Registers a custom view renderer.

Parameters

string $extension Extension handled by the renderer
Closure|string $renderer Renderer class or closure that creates a renderer instance

Return Value

ViewFactory

at line 154
ViewFactory assign(string $name, mixed $value)

Assign a global view variable that will be available in all views.

Parameters

string $name Variable name
mixed $value View variable

Return Value

ViewFactory

at line 168
ViewFactory autoAssign(string $view, callable $variables)

Assign variables that should be auto assigned to views upon creation.

Parameters

string $view View name or array of view names
callable $variables Callable that returns an array of variables

Return Value

ViewFactory

at line 183
ViewFactory clearAutoAssignVariables()

Clears the autoassign variables.

Return Value

ViewFactory

at line 197
protected array|false getViewPathAndExtension(string $view, bool $throwException = true)

Returns an array containing the view path and the renderer we should use.

Parameters

string $view View
bool $throwException Throw exception if view doesn't exist?

Return Value

array|false

at line 235
protected RendererInterface rendererFactory(Closure|string $renderer)

Creates a renderer instance.

Parameters

Closure|string $renderer Renderer class or closure

Return Value

RendererInterface

at line 246
protected RendererInterface resolveRenderer(string $extension)

Returns a renderer instance.

Parameters

string $extension Extension associated with the renderer

Return Value

RendererInterface

at line 262
bool exists(string $view)

Returns TRUE if the view exists and FALSE if not.

Parameters

string $view View

Return Value

bool

at line 273
protected array getAutoAssignVariablesForView(string $view)

Returns view specific auto assign variables.

Parameters

string $view View

Return Value

array

at line 289
protected array getAutoAssignVariables(string $view)

Returns auto assign variables for a view.

Parameters

string $view View

Return Value

array

at line 301
protected array mergeVariables(string $view, array $variables)

Returns array where variables have been merged in order of importance.

Parameters

string $view View
array $variables View variables

Return Value

array

at line 313
View create(string $view, array $variables = [])

Creates and returns a view instance.

Parameters

string $view View
array $variables View variables

Return Value

View

at line 327
string render(string $view, array $variables = [])

Creates and returns a rendered view.

Parameters

string $view View
array $variables View variables

Return Value

string