IncludeTracker

The IncludeTracker class dynamically includes PHP files and wraps their output in identifiable HTML containers using unique pp-section-id attributes. This enables precise section tracking and updates in dynamic applications.

Purpose

It provides a centralized mechanism to include external PHP files (like views or partials) using various include modes, while registering them internally with a unique section ID. This is useful for rendering dynamic blocks and tracking updates within a reactive frontend.

Static Properties

  • static array $sections: Stores a map of included file paths and their wrapped HTML content.

Methods

  • render(string $filePath, string $mode = 'include_once'): Includes a file using the specified mode and wraps the output in a uniquely identified <div> tag with a pp-section-id.

Include Modes

The second argument to render() defines the inclusion behavior. Supported values are:

  • include
  • include_once
  • require
  • require_once

Usage Example

<?php
use Lib\IncludeTracker;

IncludeTracker::render('views/homepage.php');
// Optionally specify a mode
IncludeTracker::render('views/footer.php', 'require');
?>

Generated Output

When a file is rendered using this class, its HTML is wrapped like this:

<div pp-section-id="s1x2k3a">
  ...file content...
</div>

Tracking Sections

All rendered sections are stored in IncludeTracker::$sections for reference. This is useful for systems that perform DOM diffing or section-based rehydration.

Helpful Tip

You can use IncludeTracker with dynamic route builders or page-level fragments to enable targeted updates or cache resets for specific page sections.

For implementation details, refer to the file: src/Lib/IncludeTracker.php.