【问题标题】:CodeIgniter 3 html helper ul function, how to pass parameters to li'sCodeIgniter 3 html helper ul函数,如何将参数传递给li的
【发布时间】:2015-06-11 10:31:46
【问题描述】:

我正在使用 CodeIgniter 3 实现一个站点,我正在使用 HTML helper,它可以很好地满足我的需要。我的 UL 有以下数组

$links = array(
    anchor(index_page(),"Home",(uri_string() == "" ? array('class' => 'active') : '')),
    anchor("about-us","About Us",(uri_string() == "about-us" ? array('class' => 'active') : '')),
    anchor("customers","Customers",(uri_string() == "customers" ? array('class' => 'active') : '')),
    anchor("policy","Policy",(uri_string() == "policy" ? array('class' => 'active') : '')),
    anchor("contact","Contact",(uri_string() == "contact" ? array('class' => 'active') : ''))
);

$attributes_normal = array(
    'id'    => 'main_menu'
);

所以,在拥有这些数组之后,我像这样初始化我的 ul:

<?php echo ul($links, $attributes_normal); ?>

结果如下(示例中打开关于我们的部分):

<ul id="main_menu">
    <li><a href="http://mysitesurl/">Home</a></li>
    <li><a href="http://mysitesurl/about-us" class="active">About Us</a></li>
    <li><a href="http://mysitesurl/customers">Customers</a></li>
    <li><a href="http://mysitesurl/policy">Policy</a></li>
    <li><a href="http://mysitesurl/contact">Contact</a></li>
</ul>

我在这里唯一的问题是,有没有办法将活动链接条件传递给&lt;li&gt; 元素。我使用的是 Zurb Foundation 5,默认情况下它将 class="active" 添加到 &lt;li&gt; 元素,所以我想使用默认 CSS。

【问题讨论】:

    标签: php css codeigniter zurb-foundation


    【解决方案1】:

    如果不覆盖默认的_list 辅助函数,则不能。在 HTML Helper 的源代码中的 line 145 上,您可以看到 '

  • ' 是硬编码的并且不接受其他参数:
    $out .= str_repeat(' ', $depth + 2).'<li>';
    

    我可能只使用 jQuery 或 JavaScript 设置活动类,但您也可以选择覆盖默认帮助函数以生成列表(请参阅Extending Helpers)。

  • 【讨论】:

    • 我明白了,我认为在这种情况下避免 ul 函数会更容易。谢谢!
    【解决方案2】:

    您必须在 application/helpers 路径中创建 MY_html_helper.php

    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   string
     * @param   string
     * @return  string
     */
    function _list($type = 'ul', $list = array(), $attributes = '', $depth = 0, $outer = '__outer', $inner = '__inner' )
    {
        // 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;
    
            if ( $key !== $outer && $key !== $inner) {
                $out .= str_repeat(' ', $depth + 2) . '<li ' . (is_array($val) && array_key_exists($outer, $val) ? _stringify_attributes($val[$outer]) : '') . '>';
                if (!is_array($val)) {
                    $out .= $val;
                } else {
                    $out .= $_last_list_item . "\n" . _list($type, $val, (array_key_exists($inner, $val) ? $val[$inner] : ''), $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";
    }
    }
    

    现在你可以使用 ul() 函数的 netx 配置了

    $links = array(
        'Access Control' => array(
             'Users' => array(
                   '__outer' => array(
                        //Here you have the config for 'User' option 'li'
                        'class' => 'active some-class',
                        'id' => 'my-unique-id'
                   )
              ),
              'Roles',
              'Permissions',
              '__inner' => array(
                   //Here you have the config for inner ul (sub-menu)
                   'class' => 'sub-menu'
              ),
              '__outer' => array(
                   //Here you have the config for 'Access Control' option 'li'
                   'class' => 'active'
              ),
        ), 
    );
    

    __inner 和 __outer 选项不会出现在列表中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-18
      • 2015-07-14
      • 2023-04-05
      • 2015-10-30
      • 1970-01-01
      相关资源
      最近更新 更多