passage au templating blade autonome

This commit is contained in:
Gregory Letellier
2023-11-09 09:23:52 +01:00
parent 80aeacdade
commit 04e2843569
8 changed files with 104 additions and 14 deletions

View File

@@ -3,12 +3,14 @@
namespace App\Controllers;
use App\Core\Controllers\Controller;
use DateTimeZone;
class Home extends Controller
{
public function index()
{
$html = $this->renderView("index", []);
$date = new \DateTime('now', new DateTimeZone("Europe/Paris"));
$html = $this->renderView("index", ["date" => $date->format("d/m/Y H:i:s")]);
$this->render($html);
}
}

View File

@@ -2,6 +2,7 @@
namespace App\Core\Controllers;
use App\Core\Kernel\Templating;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
@@ -58,15 +59,7 @@ abstract class Controller
*/
public function renderView($view, $params = []): string
{
extract($params);
$path = ROOT . DS . "views" . DS . str_replace(".", DS, $view) . ".php";
ob_start();
try {
include $path;
} catch (\Exception $e) {
}
$buffer = ltrim(ob_get_clean());
return $buffer;
return Templating::getInstance()->render($view, $params);
}
/**

View File

@@ -88,7 +88,6 @@ class Application
public function handle()
{
ob_start(null, 0, PHP_OUTPUT_HANDLER_CLEANABLE | PHP_OUTPUT_HANDLER_FLUSHABLE);
try {
$this->router->run();
} catch (\Exception $ex) {

View File

@@ -0,0 +1,33 @@
<?php
namespace App\Core\Kernel;
use eftec\bladeone\BladeOne;
class Templating
{
private $_views;
private $_cache;
private static $_instance = null;
private ?BladeOne $_blade = null;
private function __construct()
{
$this->_views = ROOT . DS . "views";
$this->_cache = ROOT . DS . "cache";
$this->_blade = new BladeOne($this->_views, $this->_cache, BladeOne::MODE_AUTO);
}
public static function getInstance()
{
if (self::$_instance == null) {
self::$_instance = new Templating();
}
return self::$_instance;
}
public function render(string $template, array $params = []): string
{
return $this->_blade->run($template, $params);
}
}