| 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 19:12] – [3.1- Inclusion de templates partiels] jcheron | slam4:php:phalcon:views [2019/08/31 14:21] (Version actuelle) – modification externe 127.0.0.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 ===== |
| <?php $this->partial("shared/ad_banner", array('id' => $site->id, 'size' => 'big')) ?> | <?php $this->partial("shared/ad_banner", array('id' => $site->id, 'size' => 'big')) ?> |
| </sxh> | </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]] |