silverstripe / silverstripe-framework

Silverstripe Framework, the MVC framework that powers Silverstripe CMS
https://www.silverstripe.org
BSD 3-Clause "New" or "Revised" License
720 stars 820 forks source link

ENH Convert arrays displayed in templates to ViewableData #11256

Closed emteknetnz closed 1 month ago

emteknetnz commented 1 month ago

Issue https://github.com/silverstripe/silverstripe-framework/pull/11244

Alternate solution to https://github.com/silverstripe/silverstripe-framework/pull/11244

Seems like it works?

PageController.php

    public function ArrayListTest()
    {
        return new ArrayList(['a_list_1', 'a_list_2', 'a_list_3']);
    }

    public function NestedArrayListTest()
    {
        return new ArrayList([
            new ArrayList(['na_list_4', 'na_list_5', 'na_list_6']),
            new ArrayList(['na_list_7', 'na_list_8', 'na_list_9']),
        ]);
    }

    public function ArrayTest() {
        return ['arr_1', 'arr_2', 'arr_3'];
    }

    public function NestedArrayTest() {
        return [
            ['narr_1', 'narr_2', 'narr_3'],
            ['narr_4', 'narr_5', 'narr_6'],
        ];
    }

    public function AssocArrayTest() {
        return [
            'Key1' => 'aa_1',
        ];
    }

    public function NestedAssocArrayTest() {
        return [
            'Key1' => [
                'Key2' => 'naa_1',
            ],
        ];
    }

Page.ss

        <% loop $ArrayListTest %>
            $Me<br>
        <% end_loop %>

        <% loop $NestedArrayListTest %>
            <% loop $Me %>
                $Me<br>
            <% end_loop %>
        <% end_loop %>

        <% loop $ArrayTest %>
            $Me<br>
        <% end_loop %>

        <% loop $NestedArrayTest %>
            <% loop $Me %>
                $Me<br>
            <% end_loop %>
        <% end_loop %>

        $AssocArrayTest.Key1<br>
        $NestedAssocArrayTest.Key1.Key2<br>
GuySartorelli commented 1 month ago

Maybe pull in the tests from https://github.com/silverstripe/silverstripe-framework/pull/11244 which should quickly show if there are any obvious bits missing here. Anywhere I have ArrayIterator in tests just reduce to plain array.

emteknetnz commented 1 month ago

Not using this PR