Skip to content
Snippets Groups Projects
Commit 26177046 authored by Sofiane Lasri's avatar Sofiane Lasri
Browse files

Ajout des sections dans les vues.

parent b9426312
Branches
No related tags found
No related merge requests found
......@@ -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);
......
......@@ -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();
}
}
......
<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
<html lang="fr">
<head>
<meta charset="utf-8">
<title>Info de l'adhérent</title>
<?=view('components.head')?>
</head>
<body class="">
@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>
<?=view('components.footer')?>
</body>
</html><?php
\ No newline at end of file
@endsection
\ No newline at end of file
<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
<html lang="fr">
<head>
<meta charset="utf-8">
<title>Info de l'auteur</title>
<?=view('components.head')?>
</head>
<body class="">
@extends('layouts.page-layout')
@section('title', 'Info de l'auteur')
@section('content')
<div class="container">
<h1>Information de l'auteur</h1>
{{ $auteur->afficher() }}
</div>
<?=view('components.footer')?>
</body>
</html>
@endsection
\ No newline at end of file
<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>
<?php
$test = "TESTEUU";
echo('Ceci est un <?=$test?>!');
\ No newline at end of file
<?php foreach($2 as $3) { ?>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment