我正在用CodeIgniter3实现一个站点,我正在使用HTML助手,它可以很好地满足我的需求。我的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>
我在这里唯一的问题是,是否有任何方法可以将活动链接条件传递给<li>
元素。我使用的是Zurb Foundation 5,默认情况下它会将class="active"
添加到<li>
元素中,所以我想使用默认的CSS。
如果不覆盖默认的_list
辅助函数,则不会。在HTML Helper源代码的第145行,您可以看到"<li>'是硬编码的,不接受其他参数:
$out .= str_repeat(' ', $depth + 2).'<li>';
我可能只是使用jQuery或JavaScript设置活动类,但您也可以选择覆盖用于生成列表的默认帮助程序函数(请参阅扩展帮助程序)。
您必须在应用程序/助手路径中创建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'
),
),
);
__internal和__outer选项不会出现在列表中