view()

protected function view(string $view, array $data = [], ?bool $return_as_str = null): ?string

Description

Loads and renders a view file from the current (or specified) module's views/ directory. By default, the view is output directly to the browser. Pass true as the third argument to return the rendered output as a string instead.

The view file must be a PHP file ending in .php. The $data array is extracted into variables available within the view scope.

Parameters

Parameter Type Description Default Required
view string The view file name without the .php extension. N/A Yes
data array Associative array of data to extract into the view's variable scope. [] No
return_as_str bool|null If true, returns rendered HTML as a string instead of outputting it. null (false) No

Return Value

TypeDescription
string|nullReturns the rendered HTML string when $return_as_str is true. Returns null when outputting directly.

Example Usage

PHP
<?php
// Basic view rendering
$data['page_title'] = 'Welcome';
$this->view('homepage', $data);

// View rendering with template
$data['view_file'] = 'user_profile';
$this->templates->admin($data);

// Return view as string (e.g., for emails)
$html = $this->view('email_template', $data, true);