Layout
Layout
The View class is used to render views in the Hazaar MVC framework.
- Full name:
\Hazaar\View\Layout
- Parent class:
\Hazaar\View
Properties
content
private string $content
renderedViews
private ?string $renderedViews
views
private \Hazaar\View[] $views
Methods
__construct
public __construct(?string $view = null): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$view | ?string |
setContent
Sets the content of the layout.
public setContent(string $content): void
Parameters:
Parameter | Type | Description |
---|---|---|
$content | string | the content to set |
prepare
Prepares the layout for rendering.
public prepare(bool $merge_data = true): bool
This method prepares the layout by rendering all the views added to it. It adds the layout's helpers to each view, extends the view's data with the layout's data, and renders each view. If the $merge_data
parameter is set to true, it also extends the layout's data with each view's data.
Parameters:
Parameter | Type | Description |
---|---|---|
$merge_data | bool | Whether to merge the layout's data with each view's data. Default is true. |
Return Value:
returns true if the layout was prepared successfully, false if it was already prepared
render
Renders the layout and returns it as a string.
public render(array $data = []): string
If the 'prepare' flag is set to true in the view configuration, the layout will be prepared before rendering.
Parameters:
Parameter | Type | Description |
---|---|---|
$data | array | the data to render the layout with |
Return Value:
the rendered layout as a string
layout
Returns the layout content.
public layout(): string
This method prepares the views and merges the data back in, if necessary. The layout content is then returned as a string.
Return Value:
the layout content
add
Add a view to the layout.
public add(string|\Hazaar\View $view, string $key = null): \Hazaar\View
This method will add a view based on the supplied argument. If the argument is a string a new Hazaar\View object is created using the view file named in the argument. Alterntively, the argument can be a Hazaar\View object which will simply then be added to the layout.
Parameters:
Parameter | Type | Description |
---|---|---|
$view | string|\Hazaar\View | A string naming the view to load, or an existing Hazaar_View object |
$key | string | Optional key to store the view as. Allows direct referencing later. |
remove
Removes a view from the layout.
public remove(string $key): void
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | the key of the view to remove |
Inherited methods
__construct
public __construct(string|\Hazaar\View $view, string[] $inithelpers = []): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$view | string|\Hazaar\View | |
$inithelpers | string[] |
__get
public __get(string $helper): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$helper | string |
__set
public __set(string $key, mixed $value): void
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | |
$value | mixed |
__isset
public __isset(string $key): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string |
__unset
public __unset(string $key): void
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string |
getViewPath
public static getViewPath(string $view, ?string& $name): ?string
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$view | string | |
$name | ?string |
load
public load(string $view): void
Parameters:
Parameter | Type | Description |
---|---|---|
$view | string |
getName
Returns the name of the view.
public getName(): string
getViewFile
Returns the filename that the view was loaded from.
public getViewFile(): string
get
Helper/data accessor method.
public get(mixed $helper, mixed $default = null): mixed
This 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 |
---|---|---|
$helper | mixed | 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): 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): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | The name of the view data to look for |
Return Value:
true if the view data is set (even if it is set but null/empty), false otherwise
remove
Remove view data.
public remove(string $key): void
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | the name of the view data to remove |
populate
Populate view data from an array.
public populate(array $array): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array |
extend
Extend/merge existing view data with an array.
public extend(array $array): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array |
getData
Returns the entire current view data array.
public getData(): array
addHelper
Adds a helper to the view.
public addHelper(array|\Hazaar\View\Helper|string $helper, array $args = [], string $alias = null): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$helper | array|\Hazaar\View\Helper|string | the name of the helper to add |
$args | array | the arguments to pass to the helper (optional) |
$alias | string | the alias for the helper (optional) |
Return Value:
true if the helper was added, false if the helper could not be found
hasHelper
Tests if a view helper has been loaded in this view.
public hasHelper(string $helper): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$helper | string | The name of the view helper |
getHelpers
Returns a list of all currently loaded view helpers.
public getHelpers(): array
removeHelper
Remove a loaded view helper.
public removeHelper(string $helper): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$helper | string | Returns true if the helper was unloaded. False if the view helper is not loaded to begin with. |
getHelper
Retrieve a loaded view helper object.
public getHelper(string $key): ?\Hazaar\View\Helper
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | The name of the view helper |
partial
Render a partial view in the current view.
public partial(string $view, array|bool $data = null, bool $mergedata = false): string
This method can be called from inside a view source file to include another view source file.
Parameters:
Parameter | Type | Description |
---|---|---|
$view | string | The name of the view to include, relative to the current view. This means that if the view is in the same directory, it is possible to just name the view. If it is in a sub directly, include the path relative to the current view. Using parent references (..) will also work. |
$data | array|bool | The data parameter can be either TRUE to indicate that all view data should be passed to the partial view, or an array of data to pass instead. By default, no view data is passed to the partial view. |
$mergedata | bool |
Return Value:
The rendered view output will be returned. This can then be echo'd directly to the client.
preparePartial
Prepare a partial view for later rendering.
public preparePartial(string $view, array|bool $data = null): void
This method is similar to the partial
method, but instead of rendering the view immediately, it will prepare the view for rendering later.
Parameters:
Parameter | Type | Description |
---|---|---|
$view | string | The name of the view to include, relative to the current view. This means that if the view is in the same directory, it is possible to just name the view. If it is in a sub directly, include the path relative to the current view. Using parent references (..) will also work. |
$data | array|bool | The data parameter can be either TRUE to indicate that all view data should be passed to the |
setRequiresParam
Add a required parameter to the view.
public setRequiresParam(array $array): void
This is used to add a required parameter to the view. If the parameter is not set when the view is rendered, an exception will be thrown.
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array | an array of required parameter names |
partialLoop
Render a partial view multiple times on an array.
public partialLoop(string $view, array $data): string
This basically calls $this->partial
for each element in an array
Parameters:
Parameter | Type | Description |
---|---|---|
$view | string | the partial view to render |
$data | array | a data array, usually multi-dimensional, that each element will be passed to the partial view |
Return Value:
the rendered view output
url
Generates a URL based on the provided controller, action, parameters, and absolute flag.
public url(string $controller = null, string $action = null, array $params = [], bool $absolute = false): \Hazaar\Application\URL
Parameters:
Parameter | Type | Description |
---|---|---|
$controller | string | the name of the controller |
$action | string | the name of the action |
$params | array | an array of parameters to be included in the URL |
$absolute | bool | determines whether the generated URL should be absolute or relative |
Return Value:
the generated URL
date
Returns a date string formatted to the current set date format.
public date(\Hazaar\Date|string $date): string
Parameters:
Parameter | Type | Description |
---|---|---|
$date | \Hazaar\Date|string |
timestamp
Return a date/time type as a timestamp string.
public static timestamp(\Hazaar\Date|string $value): string
This is for making it quick and easy to output consistent timestamp strings.
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$value | \Hazaar\Date|string |
datetime
Return a formatted date as a string.
public static datetime(mixed $value, mixed $format = null): string
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$value | mixed | This can be practically any date type. Either a \Hazaar\Date object, epoch int, or even a string. |
$format | mixed | Optionally specify the format to display the date. Otherwise the current default is used. |
Return Value:
the nicely formatted datetime string
offsetExists
public offsetExists(mixed $offset): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$offset | mixed |
offsetGet
public offsetGet(mixed $offset): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$offset | mixed |
offsetSet
public offsetSet(mixed $offset, mixed $value): void
Parameters:
Parameter | Type | Description |
---|---|---|
$offset | mixed | |
$value | mixed |
offsetUnset
public offsetUnset(mixed $offset): void
Parameters:
Parameter | Type | Description |
---|---|---|
$offset | mixed |
findHelper
Find a helper class for the given name.
private findHelper(string $name): null|string
This method searches for view helper classes in the specified search paths. The order of the search paths is important because it allows apps to override built-in helpers.
Parameters:
Parameter | Type | Description |
---|---|---|
$name | string | the name of the helper class to find |
Return Value:
the fully qualified class name of the helper, or null if not found
Automatically generated on 2024-11-14