Array to template

templates
Tags: #<Tag:0x00007f10c8847968>

#1

Silverstripe Version: 4.4.1

Should be simple but I think I overlook something.

I have a function in the page controller with an array as output:

    public function Address() {

            $output = array(
                'zip' => '2500',
                'street' => 'Main Street',
                'number' => '99'
            );

            return $output;

    }

Than in the template I expected that I can get the data this way:

$Address.zip
$Address.street

or

<% loop $Address %>
     $zip
     $street
<% end_loop %>

But both doesn’t work.
Also tried with ArrayList:

    public function Address() {

            $output = array(
                 'zip' => '2500',
                 'street' => 'Main Street',
                 'number' => '99'
            );

            return new ArrayList($output);

    }

What goes wrong?


#2

Try with ArrayData instead:

public function Address() 
  {
    $output = [
      'zip' => '2500',
      'street' => 'Main Street',
      'number' => '99'
    ];

      return ArrayData::create($output);
    }

Then you should be able to use the dot syntax ($Address.zip) in your template. Alternatively, you can use the <% with %> syntax to save a bit of typing, since it’s a single object:

<% with $Address %>
  $zip
  $street
  $number
<% end_with %>

#3

Thanks, works now :+1::grinning: