admin()

public function admin(array $data): void

Description

Displays the admin theme template with provided data. This method loads the full admin panel layout, complete with header, sidebar, footer, and mobile navigation. It processes optional theme settings and additional CSS/JavaScript includes before rendering the template. The method automatically injects your view content into the admin layout using the display() helper.

Parameters

Parameter Type Description Default Required
data array An associative array containing the data to pass to the template view. Common keys include 'page_title', 'view_module', 'view_file', 'theme', 'additional_includes_top', and 'additional_includes_btm'. N/A Yes

Return Value

Type Description
void This method does not return a value. It outputs the complete HTML page directly.

Additional Includes

The admin() method automatically processes two special array keys in the $data array: additional_includes_top and additional_includes_btm. These allow you to inject additional CSS files, JavaScript files, or raw HTML into specific locations within the template.

How It Works

  • additional_includes_top - Files or HTML added to this array are inserted inside the <head> section of the page
  • additional_includes_btm - Files or HTML added to this array are inserted just before the closing </body> tag

The method automatically detects file types and generates the appropriate HTML:

  • Files ending in .css are wrapped in <link rel="stylesheet" href="..."> tags
  • Files ending in .js are wrapped in <script src="..."></script> tags
  • Full URLs and raw HTML strings are inserted exactly as provided

Example #1

The code sample below demonstrates the simplest usage - loading the admin template with just a page title. The table name is inferred from the first URL segment, and the view file would be automatically determined by the method name.

PHP
public function manage(): void {
    $data['page_title'] = 'Manage Users';
    $this->templates->admin($data);
}

Example #2

This example explicitly specifies which view file to load and which module it belongs to. This approach is recommended for clarity and maintainability.

PHP
public function dashboard(): void {
    $data['page_title'] = 'Sales Dashboard';
    $data['view_module'] = 'sales';
    $data['view_file'] = 'dashboard';
    $this->templates->admin($data);
}

Example #3

This example demonstrates how to change the color scheme by setting the 'theme' key. Available built-in themes include 'default', 'blue', 'red', and 'green'.

PHP
public function settings(): void {
    $data['page_title'] = 'Account Settings';
    $data['theme'] = 'blue';
    $this->templates->admin($data);
}

Example #4

This example shows how to add additional CSS and JavaScript files that are specific to a particular page. Files are added to the 'additional_includes_top' array for assets that should load in the head section.

PHP
public function reports(): void {
    $data['page_title'] = 'Monthly Reports';
    $data['additional_includes_top'][] = 'css/charts.css';
    $data['additional_includes_top'][] = 'https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.umd.min.js';
    $this->templates->admin($data);
}

Example #5

This example demonstrates adding JavaScript files at the bottom of the page using 'additional_includes_btm'. This is the recommended approach for scripts that don't need to be available immediately on page load.

PHP
public function dashboard(): void {
    $data['page_title'] = 'Dashboard';
    $data['additional_includes_btm'][] = 'js/dashboard-charts.js';
    $data['additional_includes_btm'][] = 'js/realtime-updates.js';
    $this->templates->admin($data);
}

Example #6

This example shows how to load assets from a specific module using the module assets trigger, and also demonstrates adding raw HTML for special meta tags.

PHP
public function gallery(): void {
    $data['page_title'] = 'Photo Gallery';
    $data['additional_includes_top'][] = 'gallery_module/css/lightbox.css';
    $data['additional_includes_top'][] = '<meta name="robots" content="noindex">';
    $data['additional_includes_btm'][] = 'gallery_module/js/lightbox-init.js';
    $this->templates->admin($data);
}

Example #7

This comprehensive example combines multiple features - custom theme, conditional asset loading, and passing data to the view.

PHP
public function edit(): void {
    $product_id = (int) segment(3);
    $data['product'] = $this->db->get_where($product_id, 'products');
    $data['page_title'] = 'Edit Product';
    $data['theme'] = 'green';
    
    // Conditionally load image cropper if product has images
    if ($data['product']->has_images) {
        $data['additional_includes_top'][] = 'css/image-cropper.css';
        $data['additional_includes_btm'][] = 'js/image-cropper.js';
    }
    
    $this->templates->admin($data);
}