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:

Read More
  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.

Related posts

Leave a Reply