sunnysideup / array2ul
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
Requires
- silverstripe/admin: ^2.0
- silverstripe/framework: ^5.0
Requires (Dev)
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
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
ExpandableArrayListthat 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-a1b2c3d4and all CSS rules are prefixed with it. - AJAX-safe. The toggle uses an inline
onclick, not a<script>tag, because script tags inserted viainnerHTMLdon'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 atthemes/<theme>/templates/Sunnysideup/ArrayToUl/View/ExpandableArrayList.ssoverrides 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:
- copy these
array2ul.yml.examplefiles into yourapp/_configfolder (where available - otherwise search forprivate static $in the module to see what can be configured) - remove the .example extension,
- delete the lines you do not care about, and
- 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.