Files
wp-bootstrap/inc/Template/TemplateController.php
magdev cb288d6e74
All checks were successful
Create Release Package / PHP Lint (push) Successful in 49s
Create Release Package / Build Release (push) Successful in 1m18s
v0.1.1 - Bootstrap frontend rendering via Twig templates
Replace FSE block markup on the frontend with proper Bootstrap 5 HTML
rendered through Twig templates. The Site Editor remains functional for
admin editing while the public site outputs Bootstrap navbar, cards,
pagination, grid layout, and responsive components.

New PHP classes: TemplateController, ContextBuilder, NavWalker
New Twig templates: 20 files (base, pages, partials, components)
Enhanced TwigService with WordPress functions and globals

Co-Authored-By: Claude <noreply@anthropic.com>
2026-02-08 15:11:00 +01:00

100 lines
2.5 KiB
PHP

<?php
/**
* Template Controller.
*
* Intercepts frontend requests and renders Twig templates
* with proper Bootstrap 5 HTML instead of FSE block markup.
*
* @package WPBootstrap
* @since 0.1.1
*/
namespace WPBootstrap\Template;
use WPBootstrap\Twig\TwigService;
class TemplateController
{
private ContextBuilder $contextBuilder;
public function __construct()
{
$this->contextBuilder = new ContextBuilder();
add_action('template_redirect', [$this, 'render']);
}
/**
* Render the appropriate Twig template for the current request.
*/
public function render(): void
{
// Skip admin, REST API, and AJAX requests.
if (is_admin() || wp_doing_ajax()) {
return;
}
if (defined('REST_REQUEST') && REST_REQUEST) {
return;
}
$template = $this->resolveTemplate();
if (! $template) {
return;
}
try {
$context = $this->contextBuilder->build();
$twig = TwigService::getInstance();
echo $twig->render($template, $context);
exit;
} catch (\Throwable $e) {
// Log the error and fall back to FSE rendering.
error_log('WP Bootstrap Twig Error: ' . $e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine());
if (defined('WP_DEBUG') && WP_DEBUG) {
wp_die(
'<h1>Template Rendering Error</h1>'
. '<p><strong>' . esc_html($e->getMessage()) . '</strong></p>'
. '<p>' . esc_html($e->getFile()) . ':' . esc_html($e->getLine()) . '</p>'
. '<pre>' . esc_html($e->getTraceAsString()) . '</pre>',
'Template Error',
['response' => 500]
);
}
}
}
/**
* Determine which Twig template to render based on WordPress conditionals.
*/
private function resolveTemplate(): ?string
{
if (is_404()) {
return 'pages/404.html.twig';
}
if (is_search()) {
return 'pages/search.html.twig';
}
if (is_singular('post')) {
return 'pages/single.html.twig';
}
if (is_page()) {
return 'pages/page.html.twig';
}
if (is_archive()) {
return 'pages/archive.html.twig';
}
if (is_home()) {
return 'pages/index.html.twig';
}
// Fallback.
return 'pages/index.html.twig';
}
}