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

Modification de la classe View, ajout des boucles foreach.

parent 63f9c2fc
Branches
No related tags found
No related merge requests found
<?php
/**
* Tentative de créer un système de template.
*/
class View
{
/**
* S'occupe d'ouvrir la template.
* @param string $viewName
* @param array $args
* @return string
*/
public static function load(string $viewName, array $args = []): string
{
$viewPathParts = explode(".", $viewName);
......@@ -11,29 +20,40 @@ class View
}
$viewPath = "resources/views" . $viewPath . ".php";
if(file_exists($viewPath)){
echo self::parse($viewPath, $args);
echo self::render(self::parse($viewPath), $args);
return "";
}else{
return "La vue " . $viewName . " est introuvable.";
}
}
public static function parse(string $viewPath, array $args = []): string
/**
* S'occuper de remplir la template
* @param string $viewPath
* @param array $args
* @return string
*/
public static function parse(string $viewPath): string
{
extract($args);
$viewContent = file_get_contents($viewPath);
preg_match_all("/\{\{(.*?)\}\}/", $viewContent, $stringConcatMatches);
// On va rechercher toutes les concaténations {{ $var }}
$viewContent = preg_replace("/\{\{(.*?)\}\}/", "<?=$1?>", $viewContent);
foreach ($stringConcatMatches as $match){
if(!empty($match) && !str_contains($match[0], "{{")){
ob_start();
eval("echo " . $match[0] . ";");
$result = ob_get_clean();
$viewContent = preg_replace("/\{\{" . preg_quote($match[0]) . "\}\}/", $result, $viewContent);
}
}
// On va rechercher tous les @foreach($var1 as $var2) @endforeach
$viewContent = preg_replace("/@foreach[ ]{0,1}\((.*?) as (.*?)\)/",
"<?php foreach($1 as $2) { ?>", $viewContent);
$viewContent = preg_replace("/@endforeach/", "<?php } ?>", $viewContent);
return $viewContent;
}
public static function render(string $viewCode, array $args = []): string
{
extract($args);
// On ouvre le buffer pour enregistrer le résultat de l'echo.
ob_start();
eval("?>" . $viewCode . "<?php");
return ob_get_clean();
}
}
\ No newline at end of file
......@@ -9,19 +9,19 @@
<h1>Liste de tous les adhérents</h1>
<p>Voici la liste de tous les adhérents</p>
<div class="d-flex flex-wrap justify-content-between">
<?php foreach ($adherents as $adherent) { ?>
@foreach($adherents as $adherent)
<div class="card mb-2" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title"><?=$adherent->getPrenomAdherent()?> <?=$adherent->getNomAdherent()?></h5>
<p class="card-text">ID: <?=$adherent->getLogin()?><br>
Date de naissance: <?=$adherent->getDateAdhesion()->format("Y-m-d")?></p>
<h5 class="card-title">{{ $adherent->getPrenomAdherent() }} {{ $adherent->getNomAdherent() }}</h5>
<p class="card-text">ID: {{ $adherent->getLogin() }}<br>
Date de naissance: {{ $adherent->getDateAdhesion()->format("Y-m-d") }}</p>
<a class="btn btn-primary"
href="index.php?action=lireAdherent&login=<?=$adherent->getLogin()?>">
href="index.php?action=lireAdherent&login={{ $adherent->getLogin() }}">
Lire les détails
</a>
</div>
</div>
<?php } ?>
@endforeach
</div>
</div>
<?=view('components.footer')?>
......
......@@ -9,19 +9,19 @@
<h1>Liste de tous les auteurs</h1>
<p>Voici la liste de tous les auteurs</p>
<div class="d-flex flex-wrap justify-content-between">
<?php foreach ($auteurs as $auteur) { ?>
@foreach ($auteurs as $auteur)
<div class="card mb-2" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title"><?=$auteur->getPrenom()?> <?=$auteur->getNom()?></h5>
<p class="card-text">ID: <?=$auteur->getNumAuteur()?><br>
Date de naissance: <?=$auteur->getAnneeNaissance()?></p>
<h5 class="card-title">{{ $auteur->getPrenom() }} {{ $auteur->getNom() }}</h5>
<p class="card-text">ID: {{ $auteur->getNumAuteur() }}<br>
Date de naissance: {{ $auteur->getAnneeNaissance() }}</p>
<a class="btn btn-primary"
href="index.php?action=lireAuteur&numAuteur=<?=$auteur->getNumAuteur()?>">
href="index.php?action=lireAuteur&numAuteur={{ $auteur->getNumAuteur() }}">
Lire les détails
</a>
</div>
</div>
<?php } ?>
@endforeach
</div>
</div>
<?=view('components.footer')?>
......
......@@ -11,4 +11,4 @@
</div>
<?=view('components.footer')?>
</body>
</html><?php
</html>
<?php
$test = "TESTEUU";
echo('Ceci est un <?=$test?>!');
<?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