Add attributes on li tags for FuelPHP ul method

33 views Asked by At

Using FuelPHP, I was wondering how to add attributes on li tags for the ul method ul($list, $style = false). https://fuelphp.com/docs/classes/html.html

Here is what I want to achieve

<ul id="sortable">
    <li id="red">red</li>
    <li id="blue">blue</li>
</ul>

Here is what I tried but no luck

public static function get_ads_order_ul(){
    $items =array(array('id'=>'red','value'=>'red'),array('id'=>'blue','value'=>'blue'));
    $attr = array('id' => 'sortable');
    return Html::ul($items, $attr);
}
1

There are 1 answers

0
alphabetF On

I ended up adding new function in /fuel/core/classes/html.php object.

$items = array('red'=>array('id'=>'red'),'blue'=>array('id'=>'blue'),'green'=> null);
$attr = array('id' => 'sortable');
# The function 'ul_custom' calls 'build_list_custom' instead of 'build_list'
return Html::ul_custom($items, $attr);
protected static function build_list_custom($type = 'ul', array $list = array(), $attr = false, $indent = '')
    {
        if ( ! is_array($list))
        {
            $result = false;
        }
        $out = '';
        foreach ($list as $key => $li_attr)
        {
                $out .= $indent."\t".html_tag('li', $li_attr, $key).PHP_EOL;
        }
        $result = $indent.html_tag($type, $attr, PHP_EOL.$out.$indent).PHP_EOL;
        return $result;
    }

result

<ul id="sortable" class="ui-sortable">
    <li id="red">red</li>
    <li id="blue">blue</li>
    <li>green</li>
</ul>