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

2
.gitignore vendored
View File

@@ -6,3 +6,5 @@ var/cache/*
var/log/* var/log/*
.env .env
.vscode .vscode
cache/
composer.lock

View File

@@ -9,7 +9,8 @@
"symfony/http-kernel": "^6.3", "symfony/http-kernel": "^6.3",
"bramus/router": "~1.6", "bramus/router": "~1.6",
"filp/whoops": "^2.15", "filp/whoops": "^2.15",
"symfony/var-dumper": "^6.3" "symfony/var-dumper": "^6.3",
"eftec/bladeone": "^4.9"
}, },
"repositories": [ "repositories": [
{ {

61
composer.lock generated
View File

@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "3000634f1714c2829d26e8342521cded", "content-hash": "08bcddce7c92998b46122aa9d64c7ad6",
"packages": [ "packages": [
{ {
"name": "bramus/router", "name": "bramus/router",
@@ -57,6 +57,65 @@
}, },
"time": "2021-11-18T19:24:07+00:00" "time": "2021-11-18T19:24:07+00:00"
}, },
{
"name": "eftec/bladeone",
"version": "4.9",
"source": {
"type": "git",
"url": "https://github.com/EFTEC/BladeOne.git",
"reference": "019036c226086fbe7591360d260067c5d82400ca"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/EFTEC/BladeOne/zipball/019036c226086fbe7591360d260067c5d82400ca",
"reference": "019036c226086fbe7591360d260067c5d82400ca",
"shasum": ""
},
"require": {
"ext-json": "*",
"php": ">=7.2.5"
},
"require-dev": {
"phpunit/phpunit": "^8.5.23"
},
"suggest": {
"eftec/bladeonehtml": "Extension to create forms",
"ext-mbstring": "This extension is used if it's active"
},
"bin": [
"lib/bladeonecli"
],
"type": "library",
"autoload": {
"psr-4": {
"eftec\\bladeone\\": "lib/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Jorge Patricio Castro Castillo",
"email": "jcastro@eftec.cl"
}
],
"description": "The standalone version Blade Template Engine from Laravel in a single php file",
"homepage": "https://github.com/EFTEC/BladeOne",
"keywords": [
"blade",
"php",
"template",
"templating",
"view"
],
"support": {
"issues": "https://github.com/EFTEC/BladeOne/issues",
"source": "https://github.com/EFTEC/BladeOne/tree/4.9"
},
"time": "2023-05-01T12:48:42+00:00"
},
{ {
"name": "evenement/evenement", "name": "evenement/evenement",
"version": "v3.0.2", "version": "v3.0.2",

View File

@@ -3,12 +3,14 @@
namespace App\Controllers; namespace App\Controllers;
use App\Core\Controllers\Controller; use App\Core\Controllers\Controller;
use DateTimeZone;
class Home extends Controller class Home extends Controller
{ {
public function index() 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); $this->render($html);
} }
} }

View File

@@ -2,6 +2,7 @@
namespace App\Core\Controllers; namespace App\Core\Controllers;
use App\Core\Kernel\Templating;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@@ -58,15 +59,7 @@ abstract class Controller
*/ */
public function renderView($view, $params = []): string public function renderView($view, $params = []): string
{ {
extract($params); return Templating::getInstance()->render($view, $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;
} }
/** /**

View File

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

View File

@@ -30,6 +30,7 @@
<body> <body>
<div class="message-container"> <div class="message-container">
<h1>"It works!"</h1> <h1>"It works!"</h1>
<h2>{{ $date }}</h2>
</div> </div>
</body> </body>
</html> </html>