2
0

HtmlList.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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-2009 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-2009 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. throw new Zend_View_Exception('First param must be an array', $this);
  51. }
  52. $list = '';
  53. foreach ($items as $item) {
  54. if (!is_array($item)) {
  55. if ($escape) {
  56. $item = $this->view->escape($item);
  57. }
  58. $list .= '<li>' . $item . '</li>' . self::EOL;
  59. } else {
  60. if (6 < strlen($list)) {
  61. $list = substr($list, 0, strlen($list) - 6)
  62. . $this->htmlList($item, $ordered, $attribs, $escape) . '</li>' . self::EOL;
  63. } else {
  64. $list .= '<li>' . $this->htmlList($item, $ordered, $attribs, $escape) . '</li>' . self::EOL;
  65. }
  66. }
  67. }
  68. if ($attribs) {
  69. $attribs = $this->_htmlAttribs($attribs);
  70. } else {
  71. $attribs = '';
  72. }
  73. $tag = 'ul';
  74. if ($ordered) {
  75. $tag = 'ol';
  76. }
  77. return '<' . $tag . $attribs . '>' . self::EOL . $list . '</' . $tag . '>' . self::EOL;
  78. }
  79. }