The article template

Exercise 2 of 7 in chapter 6 · 150 XP · 2 objectives checked by tests

Topics covered: Alternative syntaxGetter

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

  1. Write templates/article.php using the HTML of versHtmlComplet(), 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 the a.retour link. Since etiquette() is private, copy its link: Url::vers('/sections/' . $article->rubrique->value) and mb_strtoupper($article->rubrique->libelle()). Every value that comes from the article (title, author, text) goes through htmlspecialchars(). Since the text is private, the nephew has added a texte() method to Article to read it.
  2. In public/index.php, the article route uses Vue::rendre('article', ['article' => $article]). Import the class at the top of the file: use Gazette\Web\Vue;. The versHtmlComplet() 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().