View
View
The View class is used to render views in the Hazaar framework.
Properties
app
The application object.
public Application $appdata
Data that is accessible from the view.
protected array $datahelpers
View Helpers.
protected array $helpersfunctionHandlers
Array of function handlers. These can be either closures or objects that provide public methods.
protected array $functionHandlersfunctions
Array of functions that can be called from the view.
protected array $functionsviewFile
public string $viewFileMethods
__construct
Constructs a new View instance.
public __construct(View $view, array $viewData): voidParameters
| Parameter | Type | Description |
|---|---|---|
$view | View | 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): ?HelperParameters
| 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): voidThis 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): boolThis 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): voidThis 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): mixedThis 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): ?stringThis 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 |
load
Load a view file.
public load(string $view): voidThis method will load a view file from disk. The view file can be either a PHP file or a Smarty template file.
Parameters
| Parameter | Type | Description |
|---|---|---|
$view | string | the name of the view to load |
getViewFile
Retrieves the path to the view file associated with this instance.
public getViewFile(): stringget
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): voidParameters
| 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): boolParameters
| 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): voidParameters
| 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): voidParameters
| 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): voidThis 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(): voidsetHelpers
Sets the helpers for the view.
public setHelpers(array $helpers): voidThis 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): ?HelperThis 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): ?HelperIf 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): stringIf 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): boolThis 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): mixedThis 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): voidIf 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): voidThis 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(mixed $handler): voidFunction 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 | mixed | The function handler object to register |
registerFunction
Register a custom function with the view.
public registerFunction(string $name, callable $function): voidCustom 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): voidParameters
| Parameter | Type | Description |
|---|---|---|
$handlers | array |
setFunctions
Sets the functions, overwriting any existing functions.
public setFunctions(array $functions): voidParameters
| Parameter | Type | Description |
|---|---|---|
$functions | array | The functions to set |
Generated by Hazaar API Doc Generator on Sun, 01 Mar 2026 09:22:25 +0000