如何通过检查当前函数名称添加 "active" 的 class
How to add a class of "active" by checking current function name
我正在尝试通过检查用户正在使用哪个功能来向列表项添加 css class of "active" 我正在使用 codeigniter 构建 cms 并使用 class 用于浏览网站的名称和功能
class Main extends CI_Controller {
public function index(){
// Get all Articles
$data['title'] = 'Latest Articles';
$data['main_content'] = 'main';
$this->load->view('layouts/main', $data);
}
public function article($slug) {
// Get Article details
$data['article'] = $this->Games_model->get_game_details($slug);
$data['title'] = $data['article']->title;
$data['main_content'] = 'article';
$this->load->view('layouts/main', $data);
}
}
我正在使用此代码创建一个 class 主目录,以便用户可以使用此 url "localhost/cms/main"
或 "localhost/cms/"
访问我的站点
我想通过检查我们在
上的哪个功能来添加 class
例如
如果我们在 "localhost/cms/"
我想添加一个 class 到家 link
否则我想在文章中添加一个 class 或者我在
上的哪个函数
希望你明白我想在这里说什么
所以有什么帮助 Folkss ??
如果我没理解错的话,您已经在 $data['main_content']
中设置了活动页面,您应该能够在您的视图(您的导航链接,或任何您想要的地方)中执行以下操作:
<a <?php echo $main_content=='main'?' class="active"':''?> href="your_main_url">Main</a>
<a <?php echo $main_content=='article'?' class="active"':''?> href="your_articles_url">Articles</a>
这基本上会检查您设置并传递给控制器中查看的 $data['main_content']
变量,并为活动页面打印 class="active"
。
正如 Hearner 已经评论过的那样,您可以使用 $data
数组来添加可以发送到模板的额外数据。在您的情况下,您还可以使用 $data['main_content']
项目来指定当前页面。
以菜单也是单独模板的方式构建模板,否则您需要在每个页面模板上检查当前活动页面。
所以给你举个例子:
// your navigation template file
<ul class="nav">
<li <?php echo ($main_content === 'main' ? 'class="active"' : '') ?>><a href="..">Main</a></li>
<li <?php echo ($main_content === 'article' ? 'class="active"' : '') ?>><a href="..">Acticles</a></li>
</ul>
你可以使用 uri 段和路由
在routes.php
$route['main'] = 'main/index';
$route['article'] = 'main/article';
可见
<a <?php echo ($this->uri->segment(1)=='main') ? 'class="active"':''; ?> href="<?php echo base_url('main'); ?>">Main</a>
<a <?php echo ($this->uri->segment(1)=='article') ? 'class="active"':''; ?> href="<?php echo base_url('article'); ?>">Articles</a>
有关更多详细信息,请查看文档,了解路线 click here and for uri segment click here
在 Codeigniter
中获取 class 的名称
$this->router->fetch_class();
在 Codeigniter 中获取方法名称
$this->router->fetch_method();
我正在尝试通过检查用户正在使用哪个功能来向列表项添加 css class of "active" 我正在使用 codeigniter 构建 cms 并使用 class 用于浏览网站的名称和功能
class Main extends CI_Controller {
public function index(){
// Get all Articles
$data['title'] = 'Latest Articles';
$data['main_content'] = 'main';
$this->load->view('layouts/main', $data);
}
public function article($slug) {
// Get Article details
$data['article'] = $this->Games_model->get_game_details($slug);
$data['title'] = $data['article']->title;
$data['main_content'] = 'article';
$this->load->view('layouts/main', $data);
}
}
我正在使用此代码创建一个 class 主目录,以便用户可以使用此 url "localhost/cms/main"
或 "localhost/cms/"
访问我的站点
我想通过检查我们在
例如
如果我们在 "localhost/cms/"
我想添加一个 class 到家 link
否则我想在文章中添加一个 class 或者我在
希望你明白我想在这里说什么
所以有什么帮助 Folkss ??
如果我没理解错的话,您已经在 $data['main_content']
中设置了活动页面,您应该能够在您的视图(您的导航链接,或任何您想要的地方)中执行以下操作:
<a <?php echo $main_content=='main'?' class="active"':''?> href="your_main_url">Main</a>
<a <?php echo $main_content=='article'?' class="active"':''?> href="your_articles_url">Articles</a>
这基本上会检查您设置并传递给控制器中查看的 $data['main_content']
变量,并为活动页面打印 class="active"
。
正如 Hearner 已经评论过的那样,您可以使用 $data
数组来添加可以发送到模板的额外数据。在您的情况下,您还可以使用 $data['main_content']
项目来指定当前页面。
以菜单也是单独模板的方式构建模板,否则您需要在每个页面模板上检查当前活动页面。
所以给你举个例子:
// your navigation template file
<ul class="nav">
<li <?php echo ($main_content === 'main' ? 'class="active"' : '') ?>><a href="..">Main</a></li>
<li <?php echo ($main_content === 'article' ? 'class="active"' : '') ?>><a href="..">Acticles</a></li>
</ul>
你可以使用 uri 段和路由
在routes.php
$route['main'] = 'main/index';
$route['article'] = 'main/article';
可见
<a <?php echo ($this->uri->segment(1)=='main') ? 'class="active"':''; ?> href="<?php echo base_url('main'); ?>">Main</a>
<a <?php echo ($this->uri->segment(1)=='article') ? 'class="active"':''; ?> href="<?php echo base_url('article'); ?>">Articles</a>
有关更多详细信息,请查看文档,了解路线 click here and for uri segment click here
在 Codeigniter
中获取 class 的名称$this->router->fetch_class();
在 Codeigniter 中获取方法名称
$this->router->fetch_method();