Twig templates as theme templates
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
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
}
}
Hi @aroberts91 nice catch, I'm working on a fix right now. I keep you up to date.
@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 Thanks! I'll give it a try and let you know asap