slam4:php:phalcon:views

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
slam4:php:phalcon:views [2015/01/10 16:17] – [2.3 Choix de vues] jcheronslam4:php:phalcon:views [2019/08/31 14:21] (Version actuelle) – modification externe 127.0.0.1
Ligne 1: Ligne 1:
 ====== Vues ====== ====== Vues ======
  
-les classes  [[http://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_View.html|Phalcon\Mvc\View]] et [[http://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_View_Simple.html|Phalcon\Mvc\View\Simple]] permettent la manipulation des vues dans le cadre du design pattern MVC.+les classes  [[https://api.phalconphp.com/class/Phalcon/Mvc/View.html|Phalcon\Mvc\View]] et [[https://api.phalconphp.com/class/Phalcon/Mvc/View/Simple.html|Phalcon\Mvc\View\Simple]] permettent la manipulation des vues dans le cadre du design pattern MVC.
  
 ===== -- Intégration des vues avec les contrôleurs ===== ===== -- Intégration des vues avec les contrôleurs =====
Ligne 196: Ligne 196:
 ===== -- Scan Simple ===== ===== -- Scan Simple =====
  
-L'alternative au scan hierarchique est offert par la classe [[http://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_View_Simple.html|Phalcon\Mvc\View\Simple]]+L'alternative au scan hierarchique est offert par la classe [[http://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_View_Simple.html|Phalcon\Mvc\View\Simple]]. 
 + 
 +Ce composant plus classique permet de choisir précisément la vue à afficher. 
 + 
 + 
 +Le composant par défaut doit être remplacé dans le fichier bootstrap lors de l'initialisation du service container $di : 
 + 
 +<sxh php;title:Partie du fichier bootstrap services.php> 
 +<?php 
 + 
 +$di->set('view', function() { 
 + 
 +    $view = new Phalcon\Mvc\View\Simple(); 
 + 
 +    $view->setViewsDir('../app/views/'); 
 + 
 +    return $view; 
 + 
 +}, true); 
 +</sxh> 
 + 
 +Démarrage de l'application et désactivation du rendering automatique : 
 + 
 +<sxh php> 
 +<?php 
 + 
 +try { 
 + 
 +    $application = new Phalcon\Mvc\Application($di); 
 + 
 +    $application->useImplicitView(false); 
 + 
 +    echo $application->handle()->getContent(); 
 + 
 +} catch (\Exception $e) { 
 +    echo $e->getMessage(); 
 +
 +</sxh> 
 + 
 +Exemples d'affichage de vues : 
 +<sxh php> 
 +<?php 
 + 
 +class PostsController extends \Phalcon\Mvc\Controller{ 
 + 
 +    public function indexAction(){ 
 +        //Render 'views-dir/index.phtml' 
 +        echo $this->view->render('index'); 
 + 
 +        //Render 'views-dir/posts/show.phtml' 
 +        echo $this->view->render('posts/show'); 
 + 
 +        //Render 'views-dir/index.phtml' passing variables 
 +        echo $this->view->render('index', array('posts' => Posts::find())); 
 + 
 +        //Render 'views-dir/posts/show.phtml' passing variables 
 +        echo $this->view->render('posts/show', array('posts' => Posts::find())); 
 +    } 
 + 
 +
 +</sxh> 
 + 
 +==== -- Inclusion de templates partiels ==== 
 + 
 +<sxh php> 
 +<div class="top"><?php $this->partial("shared/ad_banner") ?></div> 
 + 
 +<div class="content"> 
 +    <h1>Robots</h1> 
 + 
 +    <p>Check out our specials for robots:</p> 
 +    ... 
 +</div> 
 + 
 +<div class="footer"><?php $this->partial("shared/footer") ?></div> 
 +</sxh> 
 + 
 +Il est également possible de passer des variables à la vue partielle : 
 + 
 +<sxh php;gutter:false> 
 +<?php $this->partial("shared/ad_banner", array('id' => $site->id, 'size' => 'big')) ?> 
 +</sxh> 
 + 
 +==== -- Passage de variables du contrôleur à la vue ==== 
 +=== -- Variable simple === 
 + 
 +<sxh php;title:Contrôleur> 
 +   public function showAction() 
 +    { 
 +        //Passage du nombre de posts à la vue 
 +        $this->view->setVar("postsCount", 5); 
 +</sxh> 
 + 
 +<sxh php:title:Vue> 
 +echo $postCount; 
 +</sxh> 
 + 
 +=== -- Passage de plusieurs variables === 
 + 
 +<sxh php;title:Contrôleur> 
 +        $this->view->setVars(array( 
 +            'title' => $post->title, 
 +            'content' => $post->content 
 +        )); 
 +</sxh> 
 + 
 +<sxh php;title:Vue> 
 +echo $title; 
 +echo $content; 
 +</sxh> 
 + 
 +=== -- Mise en cache === 
 + 
 +voir http://docs.phalconphp.com/en/latest/reference/views.html#caching-view-fragments 
 + 
 +==== -- Evènements sur les vues ==== 
 + 
 +Les vues ont la possibilité d'envoyer des évènements à [[http://docs.phalconphp.com/en/latest/reference/events.html|l'eventsManager]] s'il est présent. 
 + 
 +^Nom ^Evènement ^Interruptible ? ^ 
 +|beforeRender |avant interprétation |Yes | 
 +|beforeRenderView |Avant interprétation d'une vue existante |Oui | 
 +|afterRenderView |Après interprétation d'une vue existante |Non | 
 +|afterRender |Après interprétation |Non | 
 +|notFoundView |Déclenché si la vue est inexistante |Non | 
 + 
 +Exemple de configuration avec eventsManager : 
 + 
 +<sxh php> 
 +$di->set('view', function() { 
 + 
 +    //création de l'eventsManager 
 +    $eventsManager = new Phalcon\Events\Manager(); 
 + 
 +    //Association d'un listener pour le type "view" 
 +    $eventsManager->attach("view", function($event, $view) { 
 +        echo $event->getType(), ' - ', $view->getActiveRenderPath(), PHP_EOL; 
 +    }); 
 + 
 +    $view = new \Phalcon\Mvc\View(); 
 +    $view->setViewsDir("../app/views/"); 
 + 
 +    //Associe l'eventsManager au composant $view 
 +    $view->setEventsManager($eventsManager); 
 + 
 +    return $view; 
 + 
 +}, true); 
 +</sxh> 
 + 
 +L'exemple suivant montre comment créé un plugin permettant de nettoyer/réparer le code HTML produit avec [[http://www.php.net/manual/en/book.tidy.php|tidy]] : 
 + 
 +<sxh php> 
 +<?php 
 + 
 +class TidyPlugin{ 
 + 
 +    public function afterRender($event, $view){ 
 + 
 +        $tidyConfig = array( 
 +            'clean' => true, 
 +            'output-xhtml' => true, 
 +            'show-body-only' => true, 
 +            'wrap' => 0, 
 +        ); 
 + 
 +        $tidy = tidy_parse_string($view->getContent(), $tidyConfig, 'UTF8'); 
 +        $tidy->cleanRepair(); 
 + 
 +        $view->setContent((string) $tidy); 
 +    } 
 + 
 +
 + 
 +//Associe le plugin en tant que listener sur l’événement afterRender 
 +$eventsManager->attach("view:afterRender", new TidyPlugin()); 
 +</sxh> 
 + 
 +La suite : [[slam4:php:phalcon:viewHelpers]]
  • slam4/php/phalcon/views.1420903038.txt.gz
  • Dernière modification : il y a 6 ans
  • (modification externe)