ZF-46: New HtmlList helper (TRAC#45 borjafp@vivirasturias.com)

Description

http://framework.zend.com/developer/ticket/45

This helper allows render a ordered/unordered list.


Index: HtmlList.php
===================================================================
--- HtmlList.php    (revision 0)
+++ HtmlList.php    (revision 0)
@@ -0,0 +1,60 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @package    Zend_View
+ * @subpackage Helper
+ * @copyright  Copyright (c) 2006 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+
+
+/**
+ * Helper to generate a "List" element
+ * 
+ * @package    Zend_View
+ * @subpackage Helper
+ * @copyright  Copyright (c) 2006 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_View_Helper_HtmlList {
+   
+   /**
+     * Generates an 'ordered/unordered' list.
+     * 
+     * @access public
+     * 
+     * @param array $items Members of the list
+     * 
+     * @param Boolean $ordered Specifies ordered/unordered
+     *
+     * 
+     * @return string The list XHTML.
+     */
+   public function htmlList($items, $ordered = false)
+   {
+       $thelist='';
+       foreach ($items as $item)
+       {
+           if (!is_array($item))
+               $thelist.='' . $item . '';
+           else
+               $thelist.=$this->htmlList($item, $ordered);
+       }
+       
+       if ($ordered)
+           return '
' . $thelist . ''; + else + return '' . $thelist . ''; + } +} \ No newline at end of file

Comments

05/11/06 13:18:23: Modified by mike

* owner changed from zend to mike.

05/12/06 11:02:57: Modified by borjafp@vivirasturias.com

New things in this version. 1 - Support for $attribs 2 - Returns concatenated

//Now returns

<

ul>

Item 1 Item a Item b Item c Item 2 Item 3

Here is the new code


+     * @param Boolean $ordered Specifies ordered/unordered
+     * 
+     * @param mixed $attribs
+     *
+     * 
+     * @return string The list XHTML.
+     */
+   public function htmlList($items, $ordered = false, $attribs=false)
+   {
+       $thelist='';
+       foreach ($items as $item)
+       {
+           if (!is_array($item))
+               $thelist.='' . $item . '';
+           else
+               $thelist.=substr($thelist,0,len($thelist)-5) . $this->htmlList($item, $ordered) . '';
+       }
+       if ($attribs) 
+           $attribs = $this->_htmlAttribs($attribs);
+       else
+           $attribs = '';
+           
+       if ($ordered)
+           return '
' . $thelist . ''; + else + return '' . $thelist . ''; + } + + /** + * Clone of FormElement.php function + * + */ + protected function _htmlAttribs($attribs) + { + $xhtml = ''; + foreach ((array) $attribs as $key => $val) { + $key = htmlspecialchars($key); + if (is_array($val)) { + $val = implode(' ', $val); + } + $val = htmlspecialchars($val); + $xhtml .= " $key=\"$val\""; + } + return $xhtml; + } +} \ No newline at end of file

Patch to include support for lists

is this patch ready to be reviewed and applied? (htmlList.txt)

Yes, patch ready and working.

HtmlList helper added to incubator with revision 1478