WizdomWeb/app/Core/View.php

72 lines
2.5 KiB
PHP

<?php
// ============================================
// File: View.php
// Version: 1.1
// Path: app/Core/View.php
// Purpose: Handles dynamic view rendering with optional layout wrapping
// Project: Wizdom Networks Website
// Usage: View::render('pages/landing', $data, 'arsha')
// ============================================
namespace WizdomNetworks\WizeWeb\Core;
use WizdomNetworks\WizeWeb\Utils\Logger;
use WizdomNetworks\WizeWeb\Utils\ErrorHandler;
/**
* View Renderer
*
* Handles view rendering, ensuring proper data passing and error handling.
*/
class View
{
/**
* Renders a view file and optionally wraps it in a layout.
*
* @param string $view The name of the view file (relative to /resources/views/).
* @param array $data Associative array of variables to pass to the view.
* @param string|null $layout The layout to use (relative to /resources/views/layouts/). Default is null (no layout).
* @throws \Exception If the view or layout file is not found.
*/
public static function render(string $view, array $data = [], ?string $layout = null): void
{
Logger::debug("Rendering view: $view");
// Extract data to make variables available in the view
extract($data);
$viewPath = realpath(__DIR__ . "/../../resources/views/" . str_replace('.', '/', $view) . ".php");
Logger::debug("Resolved view path: $viewPath");
// If using layout, resolve layout path
if ($layout) {
$layoutPath = realpath(__DIR__ . "/../../resources/views/layouts/" . $layout . ".php");
Logger::debug("Resolved layout path: $layoutPath");
if (!$layoutPath || !file_exists($layoutPath)) {
Logger::error("Layout file not found: $layout");
throw new \Exception("Layout file not found: $layout");
}
}
if (!$viewPath || !file_exists($viewPath)) {
Logger::error("View file not found: $view | Resolved path: $viewPath");
throw new \Exception("View file not found: $view");
}
// If using a layout, buffer content then inject into layout
if ($layout) {
ob_start();
include $viewPath;
$content = ob_get_clean();
include $layoutPath;
Logger::debug("Successfully rendered view: $view into layout: $layout");
} else {
include $viewPath;
Logger::debug("Successfully rendered view: $view (no layout)");
}
}
}