grayspacecoding/render-recursive

A flexible utility for rendering recursive data sets.

Maintainers

Package info

github.com/grayspacecoding/Render-Recursive

pkg:composer/grayspacecoding/render-recursive

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

dev-master 2025-12-09 16:13 UTC

This package is auto-updated.

Last update: 2026-03-09 16:53:17 UTC


README

A flexible PHP utility for rendering recursive/nested data structures with customizable output formatting.

Overview

Render Recursive provides a simple yet powerful way to traverse multidimensional arrays or hierarchical data and generate visualizations (typically HTML). It uses a callback-based approach that gives you full control over how each level and item in your data structure is rendered.

Installation

Install via Composer:

composer require grayspacecoding/render-recursive

Requirements

  • PHP >= 7.4

Basic Usage

use Grayspacecoding\RenderRecursive\RenderTiers;

$data = [
    'Item 1',
    'Item 2',
    [
        'Nested Item 1',
        'Nested Item 2',
        [
            'Deeply Nested Item'
        ]
    ],
    'Item 3'
];

$renderer = new RenderTiers();
$renderer->call($data);

This produces a nested <ul> list by default.

How It Works

The call() method accepts up to 5 parameters:

  1. $data (required) - Your multidimensional array/data structure
  2. $wrapperCallback (optional) - How to wrap the entire output
  3. $arrayCallback (optional) - How to handle items with descendants (array items)
  4. $itemCallback (optional) - How to handle leaf items (non-array items)
  5. $childDeterminer (optional) - Logic to determine if an item has children

Customization

Custom Item Rendering

$renderer->call($data, null, null, function($item) {
    echo "<div class='item'>" . htmlspecialchars($item) . "</div>";
});

Custom Array (Branch) Rendering

$renderer->call($data, null, function($item) {
    echo "<div class='branch'>";
    // Recursion happens here automatically
    echo "</div>";
}, null);

Custom Wrapper

$renderer->call($data, function($content) {
    echo "<div class='container'>" . $content . "</div>";
});

Custom Child Determiner

By default, is_array() determines if an item has children. You can customize this:

$renderer->call($data, null, null, null, function($item) {
    // Custom logic, e.g., for objects with a 'children' property
    return is_object($item) && isset($item->children) && count($item->children) > 0;
});

Complete Example

$data = [
    'Home',
    'About',
    [
        'Team',
        'History',
        [
            'Timeline',
            'Milestones'
        ]
    ],
    'Contact'
];

$renderer = new RenderTiers();

$renderer->call(
    $data,
    // Wrapper
    function($content) {
        echo "<nav class='menu'>" . $content . "</nav>";
    },
    // Array callback
    function($item) {
        echo "<ul class='submenu'>";
        // Recursion happens automatically within the library
        echo "</ul>";
    },
    // Item callback
    function($item) {
        echo "<li><a href='#'>" . htmlspecialchars($item) . "</a></li>";
    }
);

Default Behaviors

If you don't provide callbacks, the following defaults are used:

  • Wrapper: Wraps output in <ul></ul>
  • Array items: Wraps in <ul></ul> and recurses
  • Leaf items: Wraps in <li></li> with HTML-escaped content
  • Child determination: Uses is_array()

Use Cases

  • Rendering nested navigation menus
  • Displaying hierarchical category trees
  • Visualizing organizational structures
  • Converting nested data to HTML lists
  • Building recursive tree components
  • Creating nested accordion interfaces

License

MIT

Authors