View
View
The View class is used to render views in the Hazaar framework.
class View implements \ArrayAccessImplements: ArrayAccess
Properties
name
The name of the view.
public string $name = 'view'Type: string
data
Data that is accessible from the view.
protected array $dataType: array
helpers
View Helpers.
protected array $helpersType: array
functionHandlers
Array of function handlers. These can be either closures or objects that provide public methods.
protected array $functionHandlersType: array
functions
Array of functions that can be called from the view.
protected array $functionsType: array
Methods
__construct
Constructs a new View instance.
public __construct(string $view, array $viewData): voidParameters
| Parameter | Type | Description |
|---|---|---|
$view | string | the view to load, either as a string (view name/path) or an existing View instance |
$viewData | array | optional associative array of data to be passed to the view |
__get
Magic getter to retrieve a helper instance by name.
public __get(string $helper): ?HelperReturns: Hazaar\View\Helper
Parameters
| Parameter | Type | Description |
|---|---|---|
$helper | string | the name of the helper to retrieve |
__set
Magic setter method to assign a value to a property.
public __set(string $key, mixed $value): voidReturns: void
This method is triggered when writing data to inaccessible or non-existing properties. It delegates the assignment to the set() method.
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | The name of the view data |
$value | mixed | The value to set on the view data. Can be anything including strings, integers, arrays or objects. |
__isset
Checks if a given key is set in the view.
public __isset(string $key): boolReturns: bool
This magic method is triggered by calling isset() or empty() on inaccessible properties. It delegates the check to the has() method to determine if the specified key exists.
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | The name of the view data to look for |
__unset
Magic method to unset a variable from the view.
public __unset(string $key): voidReturns: void
This method is triggered when the unset() function is used on an object property. It removes the specified key from the view's data storage.
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | the name of the variable to unset |
__call
Magic method to call a function from the view.
public __call(string $method, array $args): mixedReturns: mixed
This method will call a function from the view. The function must be registered with the view using the registerFunction or registerFunctionHandler methods.
Parameters
| Parameter | Type | Description |
|---|---|---|
$method | string | The name of the method to call |
$args | array | The arguments to pass to the method |
getViewPath
Returns the path to a view file.
public getViewPath(string $view, string $name): ?stringReturns: string
This method will search for a view file in the application view path and the support view path.
Parameters
| Parameter | Type | Description |
|---|---|---|
$view | string | the name of the view to find |
$name | string | the name of the view that was found |
getViewFile
Retrieves the path to the view file associated with this instance.
public getViewFile(): ?stringReturns: string
get
Helper/data accessor method.
public get(string $key, mixed $default): voidThis will return a helper, if one exists with the name provided. Otherwise it will return any view data stored with the name.
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | the name of the helper or view data key |
$default | mixed | if neither a helper or view data is found this default value will be returned |
set
Set view data value by key.
public set(string $key, mixed $value): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | The name of the view data |
$value | mixed | The value to set on the view data. Can be anything including strings, integers, arrays or objects. |
has
Tests if view data is set with the provided key.
public has(string $key): boolReturns: bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | The name of the view data to look for |
remove
Removes a value from the view data array by its key.
public remove(string $key): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | the key of the data item to remove |
populate
Populates the view data with the provided array.
public populate(array $array): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$array | array | the data to populate the view with |
extend
Merges the given array into the current view data.
public extend(array $array): voidReturns: void
This method extends the existing data array by merging it with the provided array. If there are duplicate keys, the values from the provided array will overwrite the existing ones.
Parameters
| Parameter | Type | Description |
|---|---|---|
$array | array | the array of data to merge into the view |
getData
Retrieves the data stored in the view.
public getData(): arrayReturns: array
setHelpers
Sets the helpers for the view.
public setHelpers(array $helpers): voidReturns: void
This method replaces the current helpers with the provided array of helpers. Each helper should be an instance of the Helper class or its subclasses.
Parameters
| Parameter | Type | Description |
|---|---|---|
$helpers | array | an associative array of helpers to be set |
addHelper
Adds a view helper by name, searching through predefined namespaces.
public addHelper(string $name, $args): ?HelperReturns: Hazaar\View\Helper
This method attempts to locate and register a view helper class by searching through a list of namespace prefixes. The application's helper namespace is checked first, allowing for overrides of built-in helpers.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | the name of the helper to add |
$args |
getHelper
Retrieves a helper instance by name.
public getHelper(string $name): ?HelperReturns: Hazaar\View\Helper
If the helper with the specified name already exists in the helpers array, it is returned. Otherwise, the helper is added using the addHelper method and then returned.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | the name of the helper to retrieve |
render
Renders the view file and returns the output as a string.
public render(?array $data): stringReturns: string
If the view file has a 'tpl' extension, it uses the Smarty template engine to render the view, registering any custom functions and function handlers. Otherwise, it includes the view file directly and captures its output.
Parameters
| Parameter | Type | Description |
|---|---|---|
$data | array | optional data to be merged with the view's data for rendering |
offsetExists
Checks if the specified offset exists in the data array.
public offsetExists(mixed $offset): boolReturns: bool
This method is typically used to determine if a given key is set in the internal data storage.
Parameters
| Parameter | Type | Description |
|---|---|---|
$offset | mixed | the offset to check for existence |
offsetGet
Retrieves the value associated with the specified offset from the internal data array.
public offsetGet(mixed $offset): mixedReturns: mixed
This method allows objects of this class to be accessed using array syntax.
Parameters
| Parameter | Type | Description |
|---|---|---|
$offset | mixed | the offset to retrieve |
offsetSet
Sets a value in the internal data array at the specified offset.
public offsetSet(mixed $offset, mixed $value): voidReturns: void
If the offset is null, the value is appended to the array. Otherwise, the value is set at the given offset.
Parameters
| Parameter | Type | Description |
|---|---|---|
$offset | mixed | The offset to assign the value to. If null, value is appended. |
$value | mixed | the value to set |
offsetUnset
Unsets the value at the specified offset in the data array.
public offsetUnset(mixed $offset): voidReturns: void
This method is typically used to remove a value from the internal data storage by its key, implementing the ArrayAccess interface.
Parameters
| Parameter | Type | Description |
|---|---|---|
$offset | mixed | the key of the value to unset |
registerFunctionHandler
Register a function handler.
public registerFunctionHandler(object $handler): voidReturns: void
Function handlers are used to handle custom functions in the view template. Unlike custom functions, function handlers are objects that provide public methods that can be called from the view template.
Smarty:
{$functionName param1="value" param2="value"}PHP:
<?php $this->functionName('param1') ?>The function will be called with the parameters as an array. The function must return a string which will be inserted into the view at the point the function was called.
Parameters
| Parameter | Type | Description |
|---|---|---|
$handler | object | The function handler object to register |
registerFunction
Register a custom function with the view.
public registerFunction(string $name, callable $function): voidReturns: void
Custom functions are functions that can be called from within the view. The function can be called using the syntax:
Smarty:
{$functionName param1="value" param2="value"}PHP:
<?php $this->functionName('param1') ?>The function will be called with the parameters as an array. The function must return a string which will be inserted into the view at the point the function was called.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The name of the function to register |
$function | callable | The function to call when the function is called in the view |
setFunctionHandlers
Sets the function handlers, overwriting any existing handlers.
public setFunctionHandlers(array $handlers): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$handlers | array |
setFunctions
Sets the functions, overwriting any existing functions.
public setFunctions(array $functions): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$functions | array | The functions to set |
Generated by Hazaar API Doc Generator on Fri, 04 Sep 2026 23:32:16 +0000