Search by

sunnysideup / array2ul

sunnysideup

Convert an associative Array to a UL for display in the CMS and elsewhere

Package info

github.com/sunnysideup/silverstripe-array2ul

Type:silverstripe-vendormodule

pkg:composer/sunnysideup/array2ul

Statistics

Installs: 97

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

2.1.1 2026-06-25 20:05 UTC

This package is auto-updated.

Last update: 2026-12-05 01:37:19 UTC


README

Build Status Scrutinizer Code Quality codecov.io

Latest Stable Version License Monthly Downloads

TL;DR

Render a PHP array (associative, indexed, or nested) as an expandable HTML list. AJAX-safe: all CSS and JS are inline and scoped to a unique class per instance, so it works wherever you drop it.

Basic use

use Sunnysideup\ArrayToUl\View\ExpandableArrayList;

$data = [
    'name'    => 'Widget',
    'price'   => 19.99,
    'inStock' => true,
    'tags'    => ['new', 'featured', 'sale'],
    'meta'    => [
        'sku'     => 'WGT-001',
        'weight'  => '250g',
    ],
];

echo ExpandableArrayList::create($data);

In a .ss template:

$MyList

As a form field

use Sunnysideup\ArrayToUl\Form\Fields\ExpandableArrayListField;

public function getCMSFields()
{
    $fields = parent::getCMSFields();

    $fields->addFieldToTab(
        'Root.Debug',
        ExpandableArrayListField::create('RawData', 'Nice Heading', $this->getRawData())
            ->setCollapseAfter(10)
            ->setTitle('Raw payload')
    );

    return $fields;
}

Configuration

All setters are chainable and available on both classes:

Setter Default What it does
setCollapseAfter(int) 5 Show this many items, hide the rest behind a toggle. 0 disables collapsing.
setStartExpanded(bool) false Render with everything visible up front.
setEmptyLabel(string) '(empty)' Placeholder shown for empty arrays.
setData(array) — Replace the array after construction.

Notes

  • Associative arrays render as <dl> with a two-column CSS-grid layout (key / value). Indexed arrays render as <ul> with custom bullets.
  • Nesting is recursive. Each sub-array is rendered by its own inner ExpandableArrayList that inherits the parent's scoped CSS, so styles apply throughout but the <style> block is only emitted once.
  • Every collapsible list has its own toggle. A long sub-array deep in the tree gets its own "Show N more" button, independent of the parent.
  • Multiple instances on one page don't collide — each gets a random class like eal-a1b2c3d4 and all CSS rules are prefixed with it.
  • AJAX-safe. The toggle uses an inline onclick, not a <script> tag, because script tags inserted via innerHTML don't execute. Inline handlers always do.
  • Value formatting. Booleans, null, empty strings, and objects each get a small styled <span> so they're visually distinguishable from regular string values. Strings are HTML-escaped via the template.
  • Theme override. The template is resolved via renderWith(self::class), so dropping a file at themes/<theme>/templates/Sunnysideup/ArrayToUl/View/ExpandableArrayList.ss overrides the markup without touching the PHP.

Requirements

See composer.json for details.

Suggested modules

See composer.json for details.

Installation

composer require sunnysideup/array2ul

Configuration

In the _config folder you will find the array2ul.yml.example file that shows options for the configuration of this module.

We recommend that you:

  1. copy these array2ul.yml.example files into your app/_config folder (where available - otherwise search for private static $ in the module to see what can be configured)
  2. remove the .example extension,
  3. delete the lines you do not care about, and
  4. adjust the configurations that you would like to use.

Contributing

We welcome any contributions. See CONTRIBUTING.md for more details.

Paid assistance

You can pay us to create an improved / adapted version of this module for your own projects. Please contact us if you like to find out more: www.sunnysideup.co.nz. For example, we can write tests for this module.

Author

Sunny Side Up Ltd.

Looking for Silverstripe Modules?

To find other modules, please visit ssmods.com.