Reputation: 1117
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.
Upvotes: 1
Views: 956
Reputation: 1082
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 %>
Upvotes: 1