Accessing array in a template without a loop

Silverstripe Version: 4.1

I have found away to do this with a controller function but I thought it was worth asking for my own understanding.

In the controller I made an array keyed with a category id:

$urls[$category->ID] = $page->URLSegment;

And sent it to the template with $this->customise([‘Urls’] => $urls)

In the template I did:

<% loop $Categories %>
    <div class="col-12 col-sm-6 d-flex">
        <a href="/$Top.getUrl($ID)" class="category-block">
            <div class="category-icon">$IconSVG.RAW</div>
            <div class="category-content">
                <div class="category-title">$Title</div>
                <div class="d-none d-lg-block">$Content</div>
<% end_loop %>

I’m looping the categories objects and I had hoped to access the SUrls array with the $ID of the current $Categories but I couldn’t get it done so I wrote that getUrl function in the controller to return it out of the array…

I tried $Top.$Urls.{$ID} and $Top.$Urls.${$ID}.

Obviously in php it’s simply: echo $Urls[$ID]

1 Like