display()
function display(array $data): void
Description
Displays a content view within a template. This helper function is used inside template files to inject the actual page content at a specific location. It automatically detects the view module and view file from the provided data array, builds the path to the view file, extracts the data variables, and includes the view. If the specified view file cannot be found, it displays a helpful error message indicating the expected file path.
Parameters
| Parameter | Type | Description | Default | Required |
|---|---|---|---|---|
| data | array | An associative array containing view configuration. Expected keys include 'view_module' (the module containing the view) and 'view_file' (the name of the view file without the .php extension). If 'view_module' is not provided, it defaults to the first URL segment or 'welcome'. If 'view_file' is not provided, it defaults to 'index'. | N/A | Yes |
Return Value
| Type | Description |
|---|---|
| void | This function does not return a value. It outputs the view content directly or displays an error message if the view file cannot be found. |
Example #1
The code sample below shows the most common usage of the display() function inside a template file. This line is typically placed exactly where you want the page content to appear within your template's HTML structure.
Example #2
This example demonstrates how the display() function is used within a complete template file. The function call is placed inside the main content area where the view should be injected.
Example #3
This example shows how the data array is typically structured in a controller before being passed to a template, which then uses display() to inject the view.
Here's some sample corresponding code that could be added to a template file at modules/templates/views/admin.php
Example #4
This example shows how display() automatically detects the view module from the URL when it's not explicitly provided in the data array.
For the above example to be meaningful, assume that a website visitor is on the following URL:
https://yoursite.com/welcome/about
In that scenario, the display() method will automatically use 'welcome' as the view_module.
This is because 'welcome' is the value of the first URL segment.
This means that the above code sample would serve a view file from: