53 lines
1.6 KiB
PHP
53 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace Portfolio\Source\Design\Controller;
|
|
|
|
use Portfolio\Configure\Database\Connection;
|
|
|
|
class Base
|
|
{
|
|
protected string $layout = 'portfolio/template/base.html';
|
|
protected $databaseConnection;
|
|
|
|
public function __construct(Connection $databaseConnection)
|
|
{
|
|
$this->databaseConnection = $databaseConnection;
|
|
}
|
|
|
|
public function show(string $path, array $params = null): void
|
|
{
|
|
$viewPath = dirname(__DIR__, 3) . '/template/' . str_replace('.', '/', $path) . '.html';
|
|
|
|
if (!is_file($viewPath)) {
|
|
echo "Vue introuvable : $viewPath<br>";
|
|
print_r(scandir(dirname($viewPath)));
|
|
exit;
|
|
}
|
|
|
|
$viewContent = file_get_contents($viewPath);
|
|
|
|
// Remplacer les variables dynamiques dans la vue
|
|
if ($params) {
|
|
foreach ($params as $key => $value) {
|
|
if (is_array($value) || is_object($value)) {
|
|
// Affichage propre pour les tableaux/objets
|
|
$replacement = '<pre>' . htmlspecialchars(print_r($value, true)) . '</pre>';
|
|
} else {
|
|
$replacement = htmlspecialchars((string)$value);
|
|
}
|
|
$viewContent = str_replace('{{ ' . $key . ' }}', $replacement, $viewContent);
|
|
}
|
|
}
|
|
|
|
$layoutPath = dirname(__DIR__, 3) . '/template/base.html';
|
|
|
|
if (!file_exists($layoutPath)) {
|
|
throw new \Exception("Layout introuvable : $layoutPath");
|
|
}
|
|
|
|
$layoutContent = file_get_contents($layoutPath);
|
|
$finalHtml = str_replace('{{ content }}', $viewContent, $layoutContent);
|
|
|
|
echo $finalHtml;
|
|
}
|
|
} |