Using WordPress API with Slim 3 and Twig Views

I’m trying to integrate a decoupled wordpress/wp-api to a Slim 3 framework with Twig views.

  • Ideally I want to send all posts to /posts route rendered via Twig and
  • Individual posts to /post/hello-world route

I’m having two issues for now:

  1. I want to pass the response back (queried via guzzle) to a Twig view and I’m struggling with that.

  2. The JSON response I’m getting back in the view is not an appropriate JSON response when I echo $body;

    // ROUTES
    $app->get(’/’, function ($request, $response) {
    return $this->view->render($response, ‘home.html’);
    })->setName(‘home’);

    $app->get(’/posts/’, function ($request, $response,array $args) {
    $client = new Client();
    $url =’/cms/wp-json/wp/v2/posts’;
    $res = $client->request(‘GET’,$url,array(
    ‘content-type’ => ‘application/json’
    ),array());
    $body = $res->getBody();
    // echo $body;
    return $this->view->render( $response,‘posts.html’,array(‘posts’ => $body));
    })->setName(‘posts’);

I would really like to keep using Twig with Slim instead of using Vue.js or Angular and build a SPA.

What is you actual question?
Your complain is that the returned JSON is not suitable for twig? json_decode() the response body so you can use it

Hey - thank you for your response.

Here is my question

How can I pass a an external rest api to a twig view in Slim?

You shouldnt pass the api itself, you should just fetch all the data you need and pass that along to the view.