code image
Articles » Silverstripe - how to render arrays in templates using ArrayList and ArrayData

Silverstripe - how to render arrays in templates using ArrayList and ArrayData

14 April, 2020

Rendering indexed arrays

Let’s say we have an array called '$colors'. To render this on your template you’ll need to push the array contents into an ArrayList and wrap its contents with ArrayData like so

public function Colors() {
    $colors = ['red', 'green', 'blue'];
    $colorsList = new ArrayList();
            
    foreach ($colors as $color) {
        $colorsList->push(
            new ArrayData([
                'Color' => $color
            ])
        );
    }

    return $colorsList;
}

 

Now you can render the '$colors' array onto your template by calling the method 'Colors()' in a loop like this

<% loop $Colors %>
    <span>$Color</span>
<% end_loop %>

 

Rendering multi-dimensional arrays

Assuming we have a two-dimensional array called '$books', we'll need to push each item in the array into an ArrayList like so

public function Books() {
    $books = [
        [
            'Title' => 'Book one',
            'Author' => 'John Doe',
            'Price' => 25
        ],
        [
            'Title' => 'Book two',
            'Author' => 'Mary Jane',
            'Price' => 50
        ]
    ];
    $booksList = new ArrayList();

    foreach ($books as $book) {
        $booksList->push($book);
    }

    return $booksList;
}

Since '$books' is already a two-dimensional array we don’t need to wrap its contents with ArrayData like we did in the first example. This is because ArrayList is already a wrapper for arrays or arrays of objects.

We can now render the '$books' array onto our template by calling the 'Books()' function in a loop like this

<% loop $Books %>
    <p>Title: $Title</p>
    <p>Author: $Author</p>
    <p>Price: $Price</p>
<% end_loop %>

Post your comment

Comments

No one has commented on this page yet.

RSS feed for comments on this page | RSS feed for all comments

[2025-03-13 19:11:12] manifestcache-log.WARNING: Failed to save values: fwrite(): write of 8192 bytes failed with errno=28 No space left on device {"keys":["__CACHE__"],"exception":"[object] (ErrorException(code: 0): fwrite(): write of 8192 bytes failed with errno=28 No space left on device at /srv/users/maenterawasi/apps/maenterawasi/vendor/symfony/cache/Traits/FilesystemCommonTrait.php:108)"} [] ERROR [Warning]: Unknown: write failed: No space left on device (28) IN GET /articles/silverstripe-how-to-render-arrays-in-templates-using-arraylist-and-arraydata Line 0 in Unknown Trace ===== SilverStripe\Dev\CliDebugView->renderTrace() DetailedErrorFormatter.php:119 SilverStripe\Logging\DetailedErrorFormatter->output(2, Unknown: write failed: No space left on device (28), Unknown, 0, ) DetailedErrorFormatter.php:54 SilverStripe\Logging\DetailedErrorFormatter->format(Array) AbstractProcessingHandler.php:37 Monolog\Handler\AbstractProcessingHandler->handle(Array) Logger.php:344 Monolog\Logger->addRecord(300, E_WARNING: Unknown: write failed: No space left on device (28), Array) Logger.php:614 Monolog\Logger->log(300, E_WARNING: Unknown: write failed: No space left on device (28), Array) ErrorHandler.php:160 Monolog\ErrorHandler->handleError(2, Unknown: write failed: No space left on device (28), Unknown, 0, ) ERROR [Warning]: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/srv/users/maenterawasi/tmp/maenterawasi) IN GET /articles/silverstripe-how-to-render-arrays-in-templates-using-arraylist-and-arraydata Line 0 in Unknown Trace ===== SilverStripe\Dev\CliDebugView->renderTrace() DetailedErrorFormatter.php:119 SilverStripe\Logging\DetailedErrorFormatter->output(2, Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/srv/users/maenterawasi/tmp/maenterawasi), Unknown, 0, ) DetailedErrorFormatter.php:54 SilverStripe\Logging\DetailedErrorFormatter->format(Array) AbstractProcessingHandler.php:37 Monolog\Handler\AbstractProcessingHandler->handle(Array) Logger.php:344 Monolog\Logger->addRecord(300, E_WARNING: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/srv/users/maenterawasi/tmp/maenterawasi), Array) Logger.php:614 Monolog\Logger->log(300, E_WARNING: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/srv/users/maenterawasi/tmp/maenterawasi), Array) ErrorHandler.php:160 Monolog\ErrorHandler->handleError(2, Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/srv/users/maenterawasi/tmp/maenterawasi), Unknown, 0, )