From b8edb27f1c7d96785d1f9eb27a62577bbac0428b Mon Sep 17 00:00:00 2001 From: "maxime.batista" Date: Tue, 7 Nov 2023 15:20:52 +0100 Subject: [PATCH] setup twig --- composer.json | 3 ++- public/index.php | 8 +++++++- src/Connexion.php | 2 +- src/Controller/SampleFormController.php | 26 ++++++++++++++++++++++++- src/Views/display_results.html.twig | 18 +++++++++++++++++ src/Views/sample_form.html.twig | 20 +++++++++++++++++++ 6 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 src/Views/display_results.html.twig create mode 100644 src/Views/sample_form.html.twig diff --git a/composer.json b/composer.json index c3bb579..c78fb15 100644 --- a/composer.json +++ b/composer.json @@ -8,6 +8,7 @@ "altorouter/altorouter": "1.2.0", "ext-json": "*", "ext-pdo": "*", - "ext-pdo_sqlite": "*" + "ext-pdo_sqlite": "*", + "twig/twig":"^2.0" } } \ No newline at end of file diff --git a/public/index.php b/public/index.php index 14b0756..4c5290b 100644 --- a/public/index.php +++ b/public/index.php @@ -4,6 +4,7 @@ require "../vendor/autoload.php"; require "../config.php"; require "../sql/database.php"; +use \Twig\Loader\FilesystemLoader; use App\Connexion; use App\Controller\SampleFormController; use App\Gateway\FormResultGateway; @@ -23,6 +24,9 @@ function get_base_path() { return $basePath; } +$loader = new FilesystemLoader('../src/Views/'); +$twig = new \Twig\Environment($loader); + $basePath = get_base_path(); $con = new Connexion(get_database()); @@ -30,9 +34,11 @@ $con = new Connexion(get_database()); $router = new AltoRouter(); $router->setBasePath($basePath); -$sampleFormController = new SampleFormController(new FormResultGateway($con)); +$sampleFormController = new SampleFormController(new FormResultGateway($con), $twig); $router->map("GET", "/", fn() => $sampleFormController->displayForm()); $router->map("POST", "/submit", fn() => $sampleFormController->submitForm($_POST)); +$router->map("GET", "/twig", fn() => $sampleFormController->displayFormTwig()); +$router->map("POST", "/submit-twig", fn() => $sampleFormController->submitFormTwig($_POST)); $match = $router->match(); diff --git a/src/Connexion.php b/src/Connexion.php index c8b4015..ef9c909 100644 --- a/src/Connexion.php +++ b/src/Connexion.php @@ -41,7 +41,7 @@ class Connexion { $stmnt->bindValue($name, $value[0], $value[1]); } $stmnt->execute(); - return $stmnt->fetchAll(); + return $stmnt->fetchAll(PDO::FETCH_ASSOC); } } \ No newline at end of file diff --git a/src/Controller/SampleFormController.php b/src/Controller/SampleFormController.php index 57625e9..ad77d62 100644 --- a/src/Controller/SampleFormController.php +++ b/src/Controller/SampleFormController.php @@ -4,17 +4,23 @@ namespace App\Controller; require_once __DIR__ . "/../react-display.php"; use App\Gateway\FormResultGateway; +use \Twig\Environment; +use Twig\Error\LoaderError; +use Twig\Error\RuntimeError; +use Twig\Error\SyntaxError; class SampleFormController { private FormResultGateway $gateway; + private Environment $twig; /** * @param FormResultGateway $gateway */ - public function __construct(FormResultGateway $gateway) + public function __construct(FormResultGateway $gateway, Environment $twig) { $this->gateway = $gateway; + $this->twig = $twig; } @@ -27,4 +33,22 @@ class SampleFormController { $results = ["results" => $this->gateway->listResults()]; send_react_front("views/DisplayResults.tsx", $results); } + + public function displayFormTwig() { + try { + echo $this->twig->render('sample_form.html.twig', []); + } catch (LoaderError | RuntimeError | SyntaxError $e) { + echo "Twig error: $e"; + } + } + + public function submitFormTwig(array $request) { + $this->gateway->insert($request["name"], $request["description"]); + try { + $results = $this->gateway->listResults(); + echo $this->twig->render('display_results.html.twig', ['results' => $results]); + } catch (LoaderError | RuntimeError | SyntaxError $e) { + echo "Twig error: $e"; + } + } } \ No newline at end of file diff --git a/src/Views/display_results.html.twig b/src/Views/display_results.html.twig new file mode 100644 index 0000000..6d2aef0 --- /dev/null +++ b/src/Views/display_results.html.twig @@ -0,0 +1,18 @@ + + + + + Twig view + + + +

Hello world

+ + +{% for v in results %} +

username: {{ v.name }}

+

description: {{ v.description }}

+{% endfor %} + + + \ No newline at end of file diff --git a/src/Views/sample_form.html.twig b/src/Views/sample_form.html.twig new file mode 100644 index 0000000..2ccabc1 --- /dev/null +++ b/src/Views/sample_form.html.twig @@ -0,0 +1,20 @@ + + + + + Twig view + + + +

Hello, this is a sample form made in Twig !

+ +
+ + + + + +
+ + + \ No newline at end of file -- 2.36.3