2
0

HtmlList.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * Zend_View_Helper_FormELement
  23. */
  24. require_once 'Zend/View/Helper/FormElement.php';
  25. /**
  26. * Helper for ordered and unordered lists
  27. *
  28. * @uses Zend_View_Helper_FormElement
  29. * @category Zend
  30. * @package Zend_View
  31. * @subpackage Helper
  32. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_View_Helper_HtmlList extends Zend_View_Helper_FormElement
  36. {
  37. /**
  38. * Generates a 'List' element.
  39. *
  40. * @param array $items Array with the elements of the list
  41. * @param boolean $ordered Specifies ordered/unordered list; default unordered
  42. * @param array $attribs Attributes for the ol/ul tag.
  43. * @return string The list XHTML.
  44. */
  45. public function htmlList(array $items, $ordered = false, $attribs = false, $escape = true)
  46. {
  47. if (!is_array($items)) {
  48. require_once 'Zend/View/Exception.php';
  49. throw new Zend_View_Exception('First param must be an array', $this);
  50. }
  51. $list = '';
  52. foreach ($items as $item) {
  53. if (!is_array($item)) {
  54. if ($escape) {
  55. $item = $this->view->escape($item);
  56. }
  57. $list .= '<li>' . $item . '</li>' . self::EOL;
  58. } else {
  59. if (6 < strlen($list)) {
  60. $list = substr($list, 0, strlen($list) - 6)
  61. . $this->htmlList($item, $ordered, $attribs, $escape) . '</li>' . self::EOL;
  62. } else {
  63. $list .= '<li>' . $this->htmlList($item, $ordered, $attribs, $escape) . '</li>' . self::EOL;
  64. }
  65. }
  66. }
  67. if ($attribs) {
  68. $attribs = $this->_htmlAttribs($attribs);
  69. } else {
  70. $attribs = '';
  71. }
  72. $tag = 'ul';
  73. if ($ordered) {
  74. $tag = 'ol';
  75. }
  76. return '<' . $tag . $attribs . '>' . self::EOL . $list . '</' . $tag . '>' . self::EOL;
  77. }
  78. }