将数据传递给模板并返回渲染结果

时间:2017-06-28 08:10:33

标签: php codeigniter codeigniter-3

我已阅读有关CodeIgniter的文档和搜索论坛,但未找到可能与否的答案。

我想知道是否有可能将值传递给视图处理它并返回它的值。如果可能的话怎么样?如果不是什么选择?基本上,我试图让我的导航栏动态化。提前谢谢。

这是我的控制人员:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Faculty extends CI_Controller
{
    public function index()
    {
        $data1['navbar'] = $this->_navbar(1);
        $data['nav'] = $this->load->view('layouts/nav', $data1, TRUE);
        $this->load->view('Facultypage', $data);
    }

    function _navbar($i) {
        $activebar = array('', '', '', '', '');
        $activebar[$i] = 'active';
        return $activebar;
    }
}

导航栏的模板:

<nav class="navbar navbar-default navmargin" id="navigation">
    <div class="container-fluid text-center">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand logohelper" href="Home"><img class="logo" src="assets/images/jilcs-logo.jpg" alt="JILCS LOGO"></a>
        </div>

        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav nav-helper" id="navlist">
                <li class="<?=$navbar[0];?>"><a href="index.php">Home<span class="sr-only">(current)</span></a></li>
                <li class="<?=$navbar[1];?>" id="faculty"><a href="Faculty">Faculty</a></li>
                <li class="<?=$navbar[2];?>" id="enroll"><a href="Enroll">Enroll</a></li>
                <li class="<?=$navbar[3];?>" id="news"><a href="News">News</a></li>
                <li class="<?=$navbar[4];?>" id="about"><a href="About">About us</a></li>
            </ul>
        </div>
    </div>
</nav>

1 个答案:

答案 0 :(得分:0)

在您的视图文件中,可以直接根据控制器设置活动类,后跟方法名称

实施例:           $this->uri->segment(1); // controller $thi->uri->segment(2); // method and etc..

<ul class="nav navbar-nav nav-helper" id="navlist">
<li class="<?php if($this->uri->segment(1)=='' ) { echo 'active'; } ?>"><a href="index.php">Home<span class="sr-only">(current)</span></a></li>
<li class="<?php if($this->uri->segment(2)=='Faculty') { echo 'active'; } ?>" id="faculty"><a href="Faculty">Faculty</a></li>
<li class="<?php if($this->uri->segment(2)=='Enroll') { echo 'active'; } ?>" id="enroll"><a href="Enroll">Enroll</a></li>
<li class="<?php if($this->uri->segment(2)=='News') { echo 'active'; } ?>" id="news"><a href="News">News</a></li>
<li class="<?php if($this->uri->segment(2)=='About us') { echo 'active'; } ?>" id="about"><a href="About">About us</a></li>