The “Lost in the post” page has its template, and Madame Lou wants one for an article's page: its HTML is still buried in the concatenations of versHtmlComplet() (full HTML). This one needs a little more PHP, because the photo is only displayed if the article has one.
Your mission
- Write
templates/article.phpusing the HTML ofversHtmlComplet(), classes included:<main class="lecture">, the article with its section (p.rubrique, with its link), its title, its photo (img.photo), its author (p.auteur, “By …”), its text in<p class="texte">, then thea.retourlink. Sinceetiquette()is private, copy its link:Url::vers('/sections/' . $article->rubrique->value)andmb_strtoupper($article->rubrique->libelle()). Every value that comes from the article (title, author, text) goes throughhtmlspecialchars(). Since the text is private, the nephew has added atexte()method toArticleto read it. - In
public/index.php, the article route usesVue::rendre('article', ['article' => $article]). Import the class at the top of the file:use Gazette\Web\Vue;. TheversHtmlComplet()method is no longer used: you can delete it.
A condition in a template
<?php use Gazette\Web\Url; ?> <main class="lecture"> <article class="article"> <h2><?= htmlspecialchars($article->titre) ?></h2> <?php if ($article->photo !== null): ?> <img class="photo" src="<?= Url::vers('images/' . $article->photo) ?>" alt="<?= htmlspecialchars($article->titre) ?>"> <?php endif; ?> … </article> </main>
The if is written with a colon and closed with endif;, each piece in its own PHP tag.
Reminder
In a template, the alternative syntax (if (...): … endif;, foreach (...): … endforeach;) reads better than curly braces lost in the middle of the HTML.
texte() is a getter: a public method that lets you read a private property, without letting you change it. The template calls it like any other method: $article->texte().