Tutoriel (deuxième partie)

Nous allons maintenant améliorer le template correspondant à notre action view, soit le fichier app/views/posts/view.php. En effet, nous allons afficher en plus du billet, une liste des commentaires associés, ainsi qu'un formulaire d'ajout de commentaire.

<div id="content">
    <h2><?= $this->post->title; ?></h2>
    <p><?= $this->post->content; ?></p>
    <strong><?= $this->post->created_on->format('%d %B %Y - %H:%M:%S'); ?></strong>
    <h3><?= $this->post->comments->count(); ?> commentaire(s)</h3>
    <div id="comments">
        <? foreach ($this->post->comments->all() as $comment) : ?>
            <h5><?= $comment->author; ?>, le <?= $comment->created_on->locale(); ?></h5>
            <p><?= $comment->content; ?></p>
        <? endforeach; ?>
        <h4>Ajouter un commentaire</h4>
        <?= form_tag(array('action' => 'add_comment', 'post_id' => $this->post->id)); ?>
            <? $f = new SFormBuilder('new_comment', new Comment()); ?>
            <p>
                <?= $f->label('author', 'Votre nom'); ?>
                <?= $f->text_field('author', array('size' => 50)); ?>
            </p>
            <p>
                <?= $f->label('content', 'Votre commentaire'); ?>
                <?= $f->text_area('content', array('cols' => 70, 'rows' => 15)); ?>
            </p>
            <p>
                <?= submit_tag('Envoyer'); ?>
            </p>
        </form>
    </div>
</div>

Vous remarquerez l'utilisation de $this→post→comments→all() : une association de type has_many ou many_to_many dispose des mêmes méthodes qu'une instance de SQuerySet, en plus de ses méthodes spécifiques.

TO BE CONTINUED

 
0_9_1/fr/tutoriel_part_2.txt · Last modified: 2009/04/03 02:27 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki