wordpress-bundle icon indicating copy to clipboard operation
wordpress-bundle copied to clipboard

Twig templates as theme templates

Open aroberts91 opened this issue 1 year ago • 4 comments

Is there a way to use twig files as themes which a user can select when editing a page in wp admin? Or does this still need to be handled by a Wordpress theme?

I would like to give the user the ability to choose between a number of templates but still have access to twigs templating functionality

aroberts91 avatar May 16 '24 09:05 aroberts91

Just to further this. I have tried to create a custom template in wordpress.yaml in the hopes this could be routed to a twig file, or I could find a way to do it:

wordpress.yaml

  template:
    page:
      test_template: 'Test Template'

But trying to set this template when editing a page throws the error:

"status": 400,
       "params": {
           "template": "template is not one of ."
       },
       "details": {
           "template": {
               "code": "rest_invalid_param",
               "message": "template is not one of .",
               "data": null
           }
       }

aroberts91 avatar May 16 '24 11:05 aroberts91

Hi @aroberts91 nice catch, I'm working on a fix right now. I keep you up to date.

jerome-barbato avatar May 31 '24 08:05 jerome-barbato

@aroberts91 the issue was in the wp-steroids plugin. I have published a new version with a fix. Please do a composer update to update it.

Here is a code sample on how to use it. In your action, you need to get the template from the post object:

public function pageAction(Post $post)
{
    $template = $post->getTemplate()?:'page';

    return $this->render('pages/'.$template.'.html.twig', [
         'post'=>$post
    ]);
}

Let me know :)

jerome-barbato avatar May 31 '24 09:05 jerome-barbato

@jerome-barbato Thanks! I'll give it a try and let you know asap

aroberts91 avatar May 31 '24 10:05 aroberts91