1

I want to put data from an extern webservice into my SilverStripe website. I can get the data in an array bij this code:

public function getBlogs(){

$service = new RestfulService("http://www.xxxxx.com/jsonservice/BlogWeb/");
$response = $service->request("getBlogs?token=xxxxx&id=250");
print_r(json_decode($response->getBody()));

}

This shows the right data array in my website. But how can I handle this data to use it in the templates, like:

<% loop getBlogs %>$Title<% end_loop %>

Thanks in advance.

brasileric
  • 1,017
  • 1
  • 8
  • 18
  • 1
    possible duplicate of [Silverstripe: Convert twitter JSON string to Dataobject to loop though in template](http://stackoverflow.com/questions/19888110/silverstripe-convert-twitter-json-string-to-dataobject-to-loop-though-in-templa) – jberculo Oct 30 '14 at 07:03

1 Answers1

1

The loop construct is designed to iterate over ArrayLists and DataLists, with each item in that list intended to be a DataObject. Since json_decode returns a PHP array of objects, your function getBlogs() will need to iterate over this array and build an ArrayList of DataObjects that describe each of your blogs.

public function getBlogs() {
        $blogs = ArrayList::create();
        if($response && $response->getStatusCode() == 200 ) {
            $data = json_decode($response->getBody());
            foreach($blogs as $blog) {
                $b = DataObject::create();
                $b->Column1 = $data->blah;
                $b->Column2 = $data->bloo;
                $blogs->push($b);
            }
        }
        return $blogs;
}

Your <% loop %> construct would then iterate over the ArrayList:

<% loop getBlogs %>
    $Me.Column1 is some column. So is $Column2.
<% end_loop %>
cryptopay
  • 1,084
  • 6
  • 7