MainLayout

The MainLayout class serves as the central hub for managing page metadata, dynamic script injection, and the overall HTML structure. It handles attribute normalization and ensures unique resource loading.

Public Properties

$title

The page title used in the HTML head.

$description

The meta description for SEO purposes.

$children

The main content body to be rendered within the layout.

$html

The final compiled HTML output string.

Script & Resource Methods

Head Management

addHeadScript(string ...$scripts): void

Adds one or more raw HTML strings (scripts, links, styles) to the head section. Automatically dedupes identical entries.

outputHeadScripts(): string

Returns all registered head scripts. Automatically injects pp-dynamic-* attributes for internal tracking.

clearHeadScripts(): void

Clears all currently registered head scripts.

Footer Management

addFooterScript(string ...$scripts): void

Adds scripts to the footer. Includes intelligent component tagging based on the caller class and duplicates prevention using content hashing.

outputFooterScripts(): string

Processes and returns all footer scripts. This method handles complex attribute parsing, kebab-case conversion for attributes, and component encapsulation logic.

clearFooterScripts(): void

Clears all footer scripts and resets the processed script cache.

Metadata

addCustomMetadata(string $key, string $value): void

Registers a custom meta tag.

getCustomMetadata(string $key): ?string

Retrieves a specific metadata value by key, or returns null if not found.

outputMetadata(): string

Generates standard meta tags (charset, viewport), the title tag, and all custom metadata.

clearCustomMetadata(): void

Removes all custom metadata entries.

Complete Usage Example

<?php
use PP\MainLayout;

// 2. Set basic properties
MainLayout::$title = 'Dashboard';
MainLayout::$description = 'User analytics dashboard';

// 3. Add resources
MainLayout::addHeadScript('<link rel="stylesheet" href="/app.css">');
MainLayout::addFooterScript(
    '<script src="/analytics.js" defer></script>',
    '<script>console.log("Component Loaded");</script>'
);

// 4. Add custom meta
MainLayout::addCustomMetadata('theme-color', '#ffffff');
?>