From 261770465a2ecdcadc5b559d4d8bb1605d21081b Mon Sep 17 00:00:00 2001 From: Sofiane Lasri <alasri250@gmail.com> Date: Sat, 22 Oct 2022 17:35:25 +0200 Subject: [PATCH] Ajout des sections dans les vues. --- TP4/ex1/app/models/View.php | 41 +++++++++++++++++-- TP4/ex1/index.php | 6 +-- .../views/adherents/les-adherents.php | 16 +++----- .../resources/views/adherents/un-adherent.php | 24 +++++------ .../resources/views/auteurs/les-auteurs.php | 16 +++----- TP4/ex1/resources/views/auteurs/un-auteur.php | 24 +++++------ .../resources/views/layouts/page-layout.php | 11 +++++ TP4/ex1/test.php | 3 +- 8 files changed, 85 insertions(+), 56 deletions(-) create mode 100644 TP4/ex1/resources/views/layouts/page-layout.php diff --git a/TP4/ex1/app/models/View.php b/TP4/ex1/app/models/View.php index 4bcc3b9..2d8f770 100644 --- a/TP4/ex1/app/models/View.php +++ b/TP4/ex1/app/models/View.php @@ -20,8 +20,7 @@ class View } $viewPath = "resources/views" . $viewPath . ".php"; if(file_exists($viewPath)){ - echo self::render(self::parse($viewPath), $args); - return ""; + return self::render(self::parse($viewPath), $args); }else{ return "La vue " . $viewName . " est introuvable."; } @@ -36,10 +35,46 @@ class View public static function parse(string $viewPath): string { $viewContent = file_get_contents($viewPath); + + // On va chercher le @extends('nom-de-la-vue') (seul le premier est pris en compte) + preg_match("/@extends[ ]{0,1}\([\"|'](.*?)[\"|']\)/", $viewContent, $extendMatch); + if(!empty($extendMatch)){ + $contentToPlaceInExtends = str_replace($extendMatch[0], "", $viewContent); + $viewContent = self::load($extendMatch[1]); + + // Maintenant on va regarder si on a pas des @yield('nom') à remplacer + // On commence avec les @section("nom", "valeur"). L'ordre est important car la second regex peut englober la première. + preg_match_all("/@section[ ]{0,1}\([ ]{0,1}[\"|'](.*?)[\"|'][ ]{0,1},[ ]{0,1}[\"|'](.*?)[\"|'][ ]{0,1}\)/", + $contentToPlaceInExtends, $inlineSectionMatches); + for($i = 0; $i<count($inlineSectionMatches[0]); $i++){ + $viewContent = preg_replace( + "/@yield[ ]{0,1}\([\"|']" . preg_quote($inlineSectionMatches[1][$i]) . "[\"|']\)/", + $inlineSectionMatches[2][$i], + $viewContent + ); + + // On supprime la directive dans le contenu mis de côté car la regex du dessous capte aussi celle-ci. + $contentToPlaceInExtends = str_replace($inlineSectionMatches[0][$i], + $inlineSectionMatches[2][$i], + $contentToPlaceInExtends); + } + + // Et @section("nom") --> @endsection + preg_match_all("/@section[ ]{0,1}\([ ]{0,1}[\"|'](.*?)[\"|'][ ]{0,1}\)((\n|.)*?)@endsection/", + $contentToPlaceInExtends, $sectionMatches); + for($i = 0; $i<count($sectionMatches[0]); $i++){ + //echo $sectionMatches[1][$i] . " - " . $sectionMatches[2][$i]; + $viewContent = preg_replace( + "/@yield[ ]{0,1}\([\"|']" . preg_quote($sectionMatches[1][$i]) . "[\"|']\)/", + $sectionMatches[2][$i], + $viewContent + ); + } + } + // On va rechercher toutes les concaténations {{ $var }} $viewContent = preg_replace("/\{\{(.*?)\}\}/", "<?=$1?>", $viewContent); - // On va rechercher tous les @foreach($var1 as $var2) @endforeach $viewContent = preg_replace("/@foreach[ ]{0,1}\((.*?) as (.*?)\)/", "<?php foreach($1 as $2) { ?>", $viewContent); diff --git a/TP4/ex1/index.php b/TP4/ex1/index.php index 4b9c230..3bf1b9c 100644 --- a/TP4/ex1/index.php +++ b/TP4/ex1/index.php @@ -13,14 +13,14 @@ require_once("controller/ControleurAdherent.php"); Database::connect(); if(empty($_GET["action"])){ - ControleurAuteur::lireAuteurs(); + echo ControleurAuteur::lireAuteurs(); }else{ if(in_array($_GET["action"], get_class_methods('ControleurAuteur'))){ $action = $_GET["action"]; - ControleurAuteur::$action(); + echo ControleurAuteur::$action(); }elseif(in_array($_GET["action"], get_class_methods('ControleurAdherent'))){ $action = $_GET["action"]; - ControleurAdherent::$action(); + echo ControleurAdherent::$action(); } } diff --git a/TP4/ex1/resources/views/adherents/les-adherents.php b/TP4/ex1/resources/views/adherents/les-adherents.php index 202482d..49444c3 100644 --- a/TP4/ex1/resources/views/adherents/les-adherents.php +++ b/TP4/ex1/resources/views/adherents/les-adherents.php @@ -1,10 +1,8 @@ -<html lang="fr"> -<head> - <meta charset="utf-8"> - <title>Liste des adhérents</title> - <?=view('components.head')?> -</head> -<body class=""> +@extends('layouts.page-layout') + +@section('title', 'Liste des adhérents') + +@section('content') <div class="container"> <h1>Liste de tous les adhérents</h1> <p>Voici la liste de tous les adhérents</p> @@ -24,6 +22,4 @@ @endforeach </div> </div> - <?=view('components.footer')?> -</body> -</html> \ No newline at end of file +@endsection \ No newline at end of file diff --git a/TP4/ex1/resources/views/adherents/un-adherent.php b/TP4/ex1/resources/views/adherents/un-adherent.php index 32e6eb1..217691b 100644 --- a/TP4/ex1/resources/views/adherents/un-adherent.php +++ b/TP4/ex1/resources/views/adherents/un-adherent.php @@ -1,14 +1,10 @@ -<html lang="fr"> -<head> - <meta charset="utf-8"> - <title>Info de l'adhérent</title> - <?=view('components.head')?> -</head> -<body class=""> -<div class="container"> - <h1>Information de l'adhérent</h1> - {{ $adherent->afficher() }} -</div> -<?=view('components.footer')?> -</body> -</html><?php \ No newline at end of file +@extends('layouts.page-layout') + +@section('title', 'Info de l'adhérent') + +@section('content') + <div class="container"> + <h1>Information de l'adhérent</h1> + {{ $adherent->afficher() }} + </div> +@endsection \ No newline at end of file diff --git a/TP4/ex1/resources/views/auteurs/les-auteurs.php b/TP4/ex1/resources/views/auteurs/les-auteurs.php index e152377..a9e2c3b 100644 --- a/TP4/ex1/resources/views/auteurs/les-auteurs.php +++ b/TP4/ex1/resources/views/auteurs/les-auteurs.php @@ -1,10 +1,8 @@ -<html lang="fr"> -<head> - <meta charset="utf-8"> - <title>Liste des auteurs</title> - <?=view('components.head')?> -</head> -<body class=""> +@extends('layouts.page-layout') + +@section('title', 'Liste des auteurs') + +@section('content') <div class="container"> <h1>Liste de tous les auteurs</h1> <p>Voici la liste de tous les auteurs</p> @@ -24,6 +22,4 @@ @endforeach </div> </div> - <?=view('components.footer')?> -</body> -</html> \ No newline at end of file +@endsection \ No newline at end of file diff --git a/TP4/ex1/resources/views/auteurs/un-auteur.php b/TP4/ex1/resources/views/auteurs/un-auteur.php index 99ddff1..37bb1ec 100644 --- a/TP4/ex1/resources/views/auteurs/un-auteur.php +++ b/TP4/ex1/resources/views/auteurs/un-auteur.php @@ -1,14 +1,10 @@ -<html lang="fr"> -<head> - <meta charset="utf-8"> - <title>Info de l'auteur</title> - <?=view('components.head')?> -</head> -<body class=""> -<div class="container"> - <h1>Information de l'auteur</h1> - {{ $auteur->afficher() }} -</div> -<?=view('components.footer')?> -</body> -</html> +@extends('layouts.page-layout') + +@section('title', 'Info de l'auteur') + +@section('content') + <div class="container"> + <h1>Information de l'auteur</h1> + {{ $auteur->afficher() }} + </div> +@endsection \ No newline at end of file diff --git a/TP4/ex1/resources/views/layouts/page-layout.php b/TP4/ex1/resources/views/layouts/page-layout.php new file mode 100644 index 0000000..f87a0a1 --- /dev/null +++ b/TP4/ex1/resources/views/layouts/page-layout.php @@ -0,0 +1,11 @@ +<html lang="fr"> +<head> + <meta charset="utf-8"> + <title>@yield("title")</title> + <?=view('components.head')?> +</head> +<body class=""> + @yield("content") + <?=view('components.footer')?> +</body> +</html> diff --git a/TP4/ex1/test.php b/TP4/ex1/test.php index b4b0768..67f33f8 100644 --- a/TP4/ex1/test.php +++ b/TP4/ex1/test.php @@ -1,4 +1,3 @@ <?php $test = "TESTEUU"; -echo('Ceci est un <?=$test?>!'); -<?php foreach($2 as $3) { ?> \ No newline at end of file +echo('Ceci est un <?=$test?>!'); \ No newline at end of file -- GitLab