Le Zend_View_Helper_HeadStyle de Zend Framework a un bug non corrigé depuis très longtemps, à savoir ZF-3406. Le bug en question empèche la création d'une balise <style> avec un attribut media contenant une liste de différents médias.

  1. $this->headStyle()->captureStart(Zend_View_Helper_Placeholder_Container_Abstract::APPEND, array('media' => 'screen'))
  2. // Displays <style type="text/css" media="screen">
  3.  
  4. $this->headStyle()->captureStart(Zend_View_Helper_Placeholder_Container_Abstract::APPEND, array('media' => array('screen', 'projection', 'tv')))
  5. // Should display <style type="text/css" media="screen projection tv">
  6. // But displays <style type="text/css">

Donc, voici un nouvel helper modifié qui fait son job :

  1. <?php
  2.  
  3. require_once 'Zend/View/Helper/HeadStyle.php';
  4.  
  5. /**
  6.  * HeadStyle helper
  7.  *
  8.  * @uses helper Zend_View_Helper
  9.  */
  10. class App_View_Helper_HeadStyle extends Zend_View_Helper_HeadStyle {
  11.  
  12. /**
  13. * Convert content and attributes into valid style tag
  14. *
  15. * @param stdClass $item Item to render
  16. * @param string $indent Indentation to use
  17. * @return string
  18. */
  19. public function itemToString(stdClass $item, $indent) {
  20. $attrString = '';
  21. if (!empty($item->attributes)) {
  22. foreach($item->attributes as $key => $value) {
  23. if (!in_array($key, $this->_optionalAttributes)) {
  24. continue;
  25. }
  26. if ('media' == $key) {
  27. if (is_string($value) && preg_match('/^[\w ]+$/', $value)) {
  28. $value = explode(' ', $value);
  29. }
  30. if (is_array($value)) {
  31. $stringValue = '';
  32. $separator = '';
  33. foreach ($value as $mediaType) {
  34. if (!in_array($mediaType, $this->_mediaTypes)) {
  35. continue;
  36. }
  37. $stringValue .= $separator.$mediaType;
  38. $separator = ' ';
  39. }
  40. $value = $stringValue;
  41. } else {
  42. if (!in_array($value, $this->_mediaTypes)) {
  43. continue;
  44. }
  45. }
  46. }
  47. $attrString .= sprintf(' %s="%s"', $key, htmlspecialchars($value));
  48. }
  49. }
  50.  
  51. $html = '<style type="text/css"' . $attrString . '>' . PHP_EOL
  52. . $indent . '<!--' . PHP_EOL . $indent . $item->content . PHP_EOL . $indent . '-->' . PHP_EOL
  53. . '</style>';
  54.  
  55. return $html;
  56. }
  57.  
  58. }

On spécifier au Broker de le charger de préférence à celui de zf :

  1. $this->view->setHelperPath(Zend_Registry::get('conf')->paths->views.'helpers/', 'App_View_Helper');

Et ça s'utilise de cette façon :

  1. $this->headStyle()->captureStart(Zend_View_Helper_Placeholder_Container_Abstract::APPEND, array('media' => array('screen', 'print', 'projection', 'tv')));
  2. $this->headStyle()->captureStart(Zend_View_Helper_Placeholder_Container_Abstract::APPEND, array('media' => 'screen print projection tv'));