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

[eluser]Design Software[/eluser]
Hi all,
i need to know how to set any attripute on a <li/> tag using function ul() or ol() from HTML Helper.
Thanks.

Marco
#2

[eluser]drewbee[/eluser]
http://ellislab.com/codeigniter/user-gui...elper.html

From the manual......
Code:
$this->load->helper('html');

$list = array(
            'red',
            'blue',
            'green',
            'yellow'
            );

$attributes = array(
                    'class' => 'boldlist',
                    'id'    => 'mylist'
                    );

echo ul($list, $attributes);

The above code will produce this:
<ul class="boldlist" id="mylist">
  <li>red</li>
  <li>blue</li>
  <li>green</li>
  <li>yellow</li>
</ul>
#3

[eluser]Design Software[/eluser]
I mean attributes to <li/> tag not <ul/> one...
#4

[eluser]drewbee[/eluser]
Oops. My apologies. I read it too fast. It looks like it does not currently support attributes on the LI level.

From the function _list which is called by function ol or ul.

Code:
foreach ($list as $key => $val)
        {
            $_last_list_item = $key;

            $out .= str_repeat(" ", $depth + 2);
            $out .= "<li>";

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

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

Perhaps feature request? It doesn't seem like it would be that hard to do, I would create another array of li attributes.

IE:

Code:
$list = array('red', 'green', 'blue' = array('light blue'));
    $li_attributes['red'] = array('id' => 'id_red');
    $li_attributes['blue']['light blue'] = array('id' => 'id_light_blue');

The value will be an array if $val is an array, so use the key instead.
Code:
foreach ($list as $key => $val)
        {
            $_last_list_item = $key;

            $out .= str_repeat(" ", $depth + 2);
                        
                        if (isset($li_attributes[$val]) && is_array($li_attributes[$val]))
                        {
                            $out .= "<li ";
                            foreach ($li_attributes[$val] AS $item => $value)
                            {
                                $out .= $item . '="' . $value . '" ';
                            }
                            $out .= ">";
                        }
                        else
                        {
                $out .= "<li>";
                        }
            if ( ! is_array($val))
            {
                $out .= $val;
            }
            else
            {
                $out .= $_last_list_item."\n";
                $out .= _list($type, $val, '', $depth + 4);
                $out .= str_repeat(" ", $depth + 2);
            }

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

Make sure you add in a third parameter to accept $li_attributes to the ol/li function and pass it to the list function.

I'm pretty sure this will work for flat arrays, not to sure how it will handle multi-dimensional arrays... Dig in!! Smile
#5

[eluser]Design Software[/eluser]
Maybe i don't understand, this is my test:
Code:
$list =     array(
                            'red',
                            'green',
                            'blue' =>     array(
                                            'light blue'
                                        )
                        );

            $attributes = array(
                                'class' => 'boldlist',
                                'id'    => 'mylist'
                                );

            $li_attributes['red'] = array(
                                        'id' => 'id_red'
                                    );

            $li_attributes['blue']['light blue'] =     array(
                                                        'id' => 'id_light_blue'
                                                    );

            echo ul($list, $attributes, $li_attributes);
And it doesn't work :S,
where i'm wrong?

Thanks
#6

[eluser]InsiteFX[/eluser]
Thats because you need to run your $list through drewbee's foreach loop above then echo $out
#7

[eluser]drewbee[/eluser]
Yup. You need to 'extend' the _list function from the form_helper
#8

[eluser]Design Software[/eluser]
Hi all,
i solved this problem by creating an helper with the following code:
Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    /**
     * Unordered List Extended
     *
     * Generates an HTML unordered list with <li/> attributes from an single array.
     *
     * @access    public
     * @param    array
     * @param    mixed
     * @param    mixed
     * @return    string
     */
    if ( ! function_exists('ul_ext')){
        function ul_ext($list, $attributes = '', $li_attributes = ''){
            
            $depth = 0;
            $out = NULL;
            $type = NULL;

            foreach($list as $key => $val):
                $_last_list_item = $key;
    
                $out .= str_repeat(" ", $depth + 2);
                
                if(@isset($li_attributes[$val]) && is_array($li_attributes[$val])):
                
                    $out .= "<li ";

                    foreach($li_attributes[$val] AS $item => $value):
                        $out .= $item . '="' . $value . '" ';
                    endforeach;
                    
                    $out .= ">";
                    
                else:
                    
                    $out .= "<li>";

                endif;

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

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

            return $out;
        }
    }
?&gt;

...it works but only with "flat" arrays.
Thanks for your interest
#9

[eluser]Lykos22[/eluser]
@Everett Myers: I'm using CI 2.1.4 and tried to follow the example you 've posted, but the _list() function doesn't seem to work properly.

What I'm trying to achieve is to create a navigation menu like this:
Code:
<ul class="page-sidebar-menu" data-auto-scroll="false" data-auto-speed="200">
         <li class="start "><a href="#"><i class="fa fa-home"></i><span class="title">Dashboard</span></a></li>
         <li>
  <a href="[removed];"><i class="fa fa-post"></i><span class="title">Posts</span><span class="arrow open"></span></a>
                <ul class="sub-menu">
                            <li><a href="#">All Posts</a></li>
                            <li><a href="#">Add New</a></li>
                </ul>
         </li>
        etc etc ...
</ul>
#10

[eluser]Lykos22[/eluser]
Eventually I found out what I was doing wrong. As the [url="http://ellislab.com/codeigniter/user-guide/general/helpers.html"]user guide[/url] says, the _list() function should be extended. So I saved it inside ./application/helpers/MY_html_helper.php, and this did the trick for me!

p.s. This is definately something that would be great to put in system folder on future versions.




Theme © iAndrew 2016 - Forum software by © MyBB