public()

public function public(array $data): void

Description

Displays the public theme template with provided data. This method loads a clean, public-facing layout designed for websites and landing pages. It processes additional CSS/JavaScript includes before rendering the template. The method automatically injects your view content into the public layout using the display() helper. Unlike the admin template, the public template provides a minimal structure, giving you maximum flexibility for front-end design.

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', '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 public() 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 public template with just a page title. The view file would be automatically determined by the method name.

PHP
public function about(): void {
    $data['page_title'] = 'About Us';
    $this->templates->public($data);
}

Example #2

This example explicitly specifies which view file to load and which module it belongs to, providing clear documentation of the template structure.

PHP
public function home(): void {
    $data['page_title'] = 'Welcome to Our Site';
    $data['view_module'] = 'welcome';
    $data['view_file'] = 'homepage';
    $this->templates->public($data);
}

Example #3

This example shows how to pass database records to the view for rendering dynamic content on a public page.

PHP
public function blog_post(): void {
    $post_id = (int) segment(2);
    $data['post'] = $this->db->get_where($post_id, 'blog_posts');
    $data['page_title'] = $data['post']->title;
    $this->templates->public($data);
}

Example #4

This example demonstrates how to add external CSS frameworks and libraries that are specific to a particular page.

PHP
public function landing_page(): void {
    $data['page_title'] = 'Special Offer';
    $data['additional_includes_top'][] = 'https://unpkg.com/[email protected]/dist/leaflet.css';
    $data['additional_includes_top'][] = 'css/landing-page.css';
    $this->templates->public($data);
}

Example #5

This example shows how to add JavaScript files at the bottom of the page for optimal loading performance.

PHP
public function contact(): void {
    $data['page_title'] = 'Contact Us';
    $data['additional_includes_btm'][] = 'https://unpkg.com/[email protected]/dist/leaflet.js';
    $data['additional_includes_btm'][] = 'js/contact-map.js';
    $this->templates->public($data);
}

Example #6

This example demonstrates loading module-specific assets using the module assets trigger, keeping your public pages organized and maintainable.

PHP
public function portfolio(): void {
    $data['page_title'] = 'Our Portfolio';
    $data['projects'] = $this->db->get('display_order', 'projects');
    $data['additional_includes_top'][] = 'portfolio_module/css/gallery.css';
    $data['additional_includes_btm'][] = 'portfolio_module/js/gallery-init.js';
    $this->templates->public($data);
}

Example #7

This comprehensive example shows how to add custom meta tags for SEO and social media sharing, along with page-specific styling and scripts.

PHP
public function article(): void {
    $article_id = (int) segment(2);
    $data['article'] = $this->db->get_where($article_id, 'articles');
    $data['page_title'] = $data['article']->title;
    
    // Add SEO and social media meta tags
    $data['additional_includes_top'][] = '<meta name="description" content="' . $data['article']->excerpt . '">';
    $data['additional_includes_top'][] = '<meta property="og:title" content="' . $data['article']->title . '">';
    $data['additional_includes_top'][] = '<meta property="og:image" content="' . $data['article']->image_url . '">';
    
    // Add syntax highlighting for code snippets
    $data['additional_includes_top'][] = 'css/syntax-highlighter.css';
    $data['additional_includes_btm'][] = 'js/syntax-highlighter.js';
    
    $this->templates->public($data);
}