CodeIgniter:如何“突出显示”用户当前所在页面的链接?

时间:2010-07-28 20:49:39

标签: php html codeigniter

CodeIgniter相当新,仍然掌握MVC方法。我只是想知道解决这个问题的最佳方法是什么:

我的导航栏突出显示当前活动的链接,如下所示:

<a href="index.hml" id="active">Index</a>
<a href="blog.hml">Blog</a>

现在,当我转到blog.html时,我希望id =“active”相应地转移。 通常我会为每个链接分配一个变量,然后将其值设置为'id =“active'。 不知何故,我不认为这是最好的方式。 有什么想法吗?

更新(2012年9月12日)自从问这个问题后,我转到Kohana并扩展了一个完全为此目的而创建的模块。现在,我需要做的就是在配置数组中指定我的菜单项,并突出显示自动发生。该模块为here

5 个答案:

答案 0 :(得分:7)

对CI不具体,这只是对当前页面名称的逻辑检查。

在CI中获取页面名称,例如

$pageName = 'blog.html';

然后你可以做以下

<a href="index.html" <?php echo $pageName == 'index.html' ? 'id="active"' : ''; ?>>Index</a>
<a href="blog.html" <?php echo $pageName == 'blog.html' ? 'id="active"' : ''; ?>>Blog</a>

答案 1 :(得分:5)

首先你不应该使用id作为那种东西,id是给页面上的每个DOM元素一个唯一的标识号,为什么,我们最好使用一个类。

Code Igniter提供了很多帮助器和类,它们成为我们的工具的一部分,可能你之前听说过“URL Segments”。

<强> $这 - &GT; URI-&GT;段(N

允许您检索特定细分。其中n是您要检索的段号。细分从左到右编号。例如,如果您的完整网址是:

http://example.com/index.php/news/local/metro/crime_is_up

细分数字是这样的:

  1. 消息
  2. 本地
  3. 地铁
  4. crime_is_up
  5. <强> http://codeigniter.com/user_guide/libraries/uri.html

    您可以使用它来检索代表您实际在浏览器上显示的活动页面的当前网址段。

答案 2 :(得分:3)

我使用@Calle示例,工作得非常好......我确实必须使用自定义网址抓取器而不是CI current_url(),因为我绕过带有.htaccess的index.php文件。我还添加了正确的属性标记。

初学者笔记 我在'helpers'中创建了这个名为'menu_helper.php'的文件并通过控制器加载它$ this-&gt; load-&gt; helper('menu');

<强>助手/ menu_helper.php

if ( ! function_exists('menu_anchor'))
{
    function menu_anchor($uri = '', $title = '', $attributes = '')
    {
        $title = (string) $title;

        if ( ! is_array($uri))
        {
            $site_url = ( ! preg_match('!^\w+://! i', $uri)) ? site_url($uri) : $uri;
        }
        else
        {
            $site_url = site_url($uri);
        }

        if ($title == '')
        {
            $title = $site_url;
        }

        if ($attributes != '')
        {
            $attributes = _parse_attributes($attributes);
        }

        $current_url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
        $attributes .= ($site_url == $current_url) ? 'class="active"' : 'class="normal"';


        return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>';
    }
}

视图文件:

<ul class="topnav love">
    <li class="topnav-1"><?=menu_anchor(base_url()."path/to/something", "menu_item")?></li>
    <li class="topnav-2"><?=menu_anchor(base_url()."path/to/something", "menu_item")?></li>
    <li class="topnav-3"><?=menu_anchor(base_url()."path/to/something", "menu_item")?></li>
    <li class="topnav-4"><?=menu_anchor(base_url()."path/to/something", "menu_item")?></li>
    <li class="topnav-5"><?=menu_anchor(base_url()."path/to/something", "menu_item")?></li>
</ul>

css:

ul.topnav li.topnav-1 a.active {     聪明的东西 }

答案 3 :(得分:3)

我使用下面的代码 - 干杯!

<?php echo ($this->uri->segment(1)=='dashboard' && $this->uri->segment(2)=='')? 'active' : ''; ?>

答案 4 :(得分:0)

我从上面的评论中看到,也许你正在寻找一些更紧凑的东西。默认情况下,Codeigniter中没有类似的功能,但它很容易制作一个。这只是我现在放在一起的东西,但它可能就是你想拥有的东西。

首先,请查看手册中的URL帮助程序。 http://codeigniter.com/user_guide/helpers/url_helper.html

专门查看锚功能并了解如何使用。您应该习惯使用此帮助程序,HTML帮助程序和表单帮助程序。它会改善你的观点。所有帮助程序都可以在系统文件夹中找到。我做的是我刚刚打开了U​​RL助手并复制了锚码,然后我在应用程序下的助手文件夹中创建了自己的助手文件。您可以阅读有关创建自己的帮助程序以及在手册中自动加载它们的更多信息。

然后我对它做了一些修改。最终结果如下:

if ( ! function_exists('menu_anchor'))
{
    function menu_anchor($uri = '', $title = '', $attributes = '')
    {
        $title = (string) $title;

        if ( ! is_array($uri))
        {
            $site_url = ( ! preg_match('!^\w+://! i', $uri)) ? site_url($uri) : $uri;
        }
        else
        {
            $site_url = site_url($uri);
        }

        if ($title == '')
        {
            $title = $site_url;
        }

        if ($attributes != '')
        {
            $attributes = _parse_attributes($attributes);
        }

    $attributes .= ($site_url == current_url()) ? ' selected' : '';

        return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>';
    }
}

正如您所看到的,它只是一行修改: $ attributes。=($ site_url == current_url())? '选中':'';

基本上,如果检索到的页面的URL与锚链接到的papge匹配,则会添加所选的类。如果您不希望所选链接链接到任何特定位置,您也可以通过将$ site_url设置为#或其他内容来轻松解决此问题。