HtmlList.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_View
  17. * @subpackage Helper
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Zend_View_Helper_FormELement
  24. */
  25. require_once 'Zend/View/Helper/FormElement.php';
  26. /**
  27. * Helper for ordered and unordered lists
  28. *
  29. * @uses Zend_View_Helper_FormElement
  30. * @category Zend
  31. * @package Zend_View
  32. * @subpackage Helper
  33. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_View_Helper_HtmlList extends Zend_View_Helper_FormElement
  37. {
  38. /**
  39. * Generates a 'List' element.
  40. *
  41. * @param array $items Array with the elements of the list
  42. * @param boolean $ordered Specifies ordered/unordered list; default unordered
  43. * @param array $attribs Attributes for the ol/ul tag.
  44. * @return string The list XHTML.
  45. */
  46. public function htmlList(array $items, $ordered = false, $attribs = false, $escape = true)
  47. {
  48. if (!is_array($items)) {
  49. require_once 'Zend/View/Exception.php';
  50. $e = new Zend_View_Exception('First param must be an array');
  51. $e->setView($this->view);
  52. throw $e;
  53. }
  54. $list = '';
  55. foreach ($items as $item) {
  56. if (!is_array($item)) {
  57. if ($escape) {
  58. $item = $this->view->escape($item);
  59. }
  60. $list .= '<li>' . $item . '</li>' . self::EOL;
  61. } else {
  62. if (6 < strlen($list)) {
  63. $list = substr($list, 0, strlen($list) - 6)
  64. . $this->htmlList($item, $ordered, $attribs, $escape) . '</li>' . self::EOL;
  65. } else {
  66. $list .= '<li>' . $this->htmlList($item, $ordered, $attribs, $escape) . '</li>' . self::EOL;
  67. }
  68. }
  69. }
  70. if ($attribs) {
  71. $attribs = $this->_htmlAttribs($attribs);
  72. } else {
  73. $attribs = '';
  74. }
  75. $tag = 'ul';
  76. if ($ordered) {
  77. $tag = 'ol';
  78. }
  79. return '<' . $tag . $attribs . '>' . self::EOL . $list . '</' . $tag . '>' . self::EOL;
  80. }
  81. }