定制 grayspacecoding/render-recursive 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

grayspacecoding/render-recursive

Composer 安装命令:

composer require grayspacecoding/render-recursive

包简介

A flexible utility for rendering recursive data sets.

README 文档

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

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-09