Welcome Guest, Not a member yet? Register   Sign In
CLASS attribute on <li/> using HTML Helper
#1

Hi all,

I had found the old topic http://forum.codeigniter.com/archive/ind...17276.html from ellislab forum regarding how to apply attributes in <li> elements or nested lists inside <li>.

I remember that one of the members of the forum had wrote a very useful extension of the _list() in order to do this, which was working n-level deep and also had provided an example on how to structure the code properly.

Unfortunately I haven't be able to find much of this archived topic, so I was wondering whether someone had this _list() extention and place it in proper [ code ] block again for better readability again.

Thanks
Reply
#2

I don't have the original, but I'd imagine it would be something like this:
PHP Code:
if ( ! function_exists('ul'))

{
    /**
     * Unordered List
     *
     * Generates an HTML unordered list from an single or multi-dimensional array.
     *
     * @param    array
     * @param    mixed
     * @param    mixed
     * @return    string
     */
    function ul($list$attributes ''$liAttributes '')
    {
        return _list('ul'$list$attributes0$liAttributes);
    }
}

// ------------------------------------------------------------------------

if ( ! function_exists('ol'))
{
    /**
     * Ordered List
     *
     * Generates an HTML ordered list from an single or multi-dimensional array.
     *
     * @param    array
     * @param    mixed
     * @param    mixed
     * @return    string
     */
    function ol($list$attributes ''$liAttributes '')
    {
        return _list('ol'$list$attributes0$liAttributes);
    }
}

// ------------------------------------------------------------------------

if ( ! function_exists('_list'))
{
    /**
     * Generates the list
     *
     * Generates an HTML ordered list from an single or multi-dimensional array.
     *
     * @param    string
     * @param    mixed
     * @param    mixed
     * @param    int
     * @param    mixed
     * @return    string
     */
    function _list($type 'ul'$list = array(), $attributes ''$depth 0$liAttributes '')
    {
        // If an array wasn't submitted there's nothing to do...
        if ( ! is_array($list))
        {
            return $list;
        }

        // Set the indentation based on the depth
        $out str_repeat(' '$depth)
            // Write the opening list tag
            .'<'.$type._stringify_attributes($attributes).">\n";


        // Cycle through the list elements.  If an array is
        // encountered we will recursively call _list()

        static $_last_list_item '';
        foreach ($list as $key => $val)
        {
            $_last_list_item $key;

            $out .= str_repeat(' '$depth 2).'<li'._stringify_attributes($liAttributes).'>';

            if ( ! is_array($val))
            {
                $out .= $val;
            }
            else
            
{
                $out .= $_last_list_item."\n"._list($type$val''$depth 4).str_repeat(' '$depth 2);
            }

            $out .= "</li>\n";
        }

        // Set the indentation for the closing tag and apply it
        return $out.str_repeat(' '$depth).'</'.$type.">\n";
    }

Reply
#3

(This post was last modified: 08-19-2015, 03:06 AM by Lykos22.)

I sit and wrote yesterday my extension on _list() function in order to do this.

I remember some parts of the script I was reffering to and tried to implement mine as this one, although I believe I have some changes and additions too.

Anyway I have uploaded it on Github, the link is this one https://github.com/Lykos22/_list.

Any thoughts or improvements are welcomed. Smile

P.S. to the CodeIgniter development team: Please consider adding this functionality to one of the upcoming versions of the framework (I mean not mine, probably you could do it in a more efficient way), I think it would be very useful to many.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB