开发者

Template Building

Working with the Template Library Here and am still a little confused on where I'm supposed to store my header and footer files and how they are formed still.

Controller:

class Kowmanager extends CI_Controller {

public function __construct()
{
    $this->load->helper('url');
    $this->load->library('tank_auth');
    $this->load->library('template');
    parent::__construct();

}

function index()
{
    if (!$this->tank_auth->is_logged_in()) {
        redirect('/auth/login/');
    } else {
        $data['user_id']    = $this->tank_auth->get_user_id();
        $data['username']   = $this->tank_auth->get_username();
        $this->load->view('welcome', $data);
    }
}

}

/* End of file kowmanager.php */
/* Locat开发者_运维知识库ion: ./application/controllers/kowmanager.php */

What I want to happen is have the header and footer files load and then also have it to where it'll load the active model because there's' login and there's register and others but those will have its own content and it'll be loaded in between the header and footer.

Edit: I'm just confused on where to put the header footer files

Does anyone have any ideas with this?


If I understand correctly, you want to load a view between a header and footer?

I had the same problem, and finally came up with the idea to use a library to do the rendering.

What I did was create a filelibraries/render.php with something like:

class render
{
    private $CI;
    function __construct ()
    {
        parent::__construct();
        $this->CI &= get_instance();
    }
    function view ($activeView, $params, $title)
    {
        $this->CI->load->view('template/header.php', array('title'=>$title));
        $this->CI->load->view($activeView, $params);
        $this->CI->load->view('template/footer.php', array('navbar'=>$this->RenderFooterNavBar()));
    }

    private function RenderFooterNavBar ()
    {
        $bits = array('Home','About Us', 'Contact'); //You could get these from anywhere
        return $this->CI->load->view('template/modules/footernavbar', array('bits'=>$bits), TRUE); //returns the rendered output of that view
    }
}

With the files like:
template/header.php:

<html>
<head>
    <title><?php echo $title; ?></title>
</head>
<body>

template/footer.php:

</body>
</html>

template/modules/footernavbar

<ul>
<?php
foreach ($bits as $item)
    echo "<li>$item</li>";
?>
</ul>

Then to use:

function index ()
{
    $this->render->view('post', $data, 'Blog Post');
}

Note, this should work with any templating system, just tweak the load->views with what your templating system uses. This is also a great way to render data that a header/footer needs, if you want to pull things from a database, just mirror what I did with the RenderFooterNavBar () function.

Hope that helps some,
Max

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜