开发者

Questions about views

I have some questions about View in Codeigniter.

How do I build up my design and how do I return it later? I would like to build my header, main content and menu开发者_运维百科, footer and more.

To build up a design and then copy it into every new view I created does not feel like a good solution. If I change something in the footer, the change takes effect for the entire website.

Does anyone have any link to any good guide how to make the best use out of view or explain in a good way here.


Your feeling is right, it's not a good idea at all. That's why most people use some kind of templating system in conjunction with their View.

I used to use this library : http://williamsconcepts.com/ci/codeigniter/libraries/template/ (used to, because I don't code much in CI anymore)

It's never given me trouble and it's pretty easy to setup.


You should make your header, footer, menu, etc. each their own view. That way when you want to change, say, the footer, you edit that view, and then each controller that uses that view will be updated.

See the CodeIgniter docs for more info on multiple view files.


You have two-and-a-half options here (assuming you don't wish to install a plugin of some kind).

Option 1 is to, in your views, use <?php $this->load->view('header'); ?> and so on.

Option 2 is to, in your controller, override the output function, which takes one argument (the content of the page) and manipulate it from there (adding views and whatnot). Option 2.5 is to override the base CI_Controller and implement a standard override of the output function same as above.

Check the documentation on the output class before doing option 2/2.5; you will need to set the output content to null or an empty string before echoing, or the content will appear twice.


Coming out of the controller

   if($query->result())
{
    **$data['blog']** = $query->result();
}
    $data['title'] = 'LemonRose';
    $data['content'] = 'home/home_content'; //this is the content section, a separate view from header and footer
    //$this->output->cache(60);
    **$this->load->view('template1', $data);** 
}

Note template1

Template 1 (this is the entire page)

$this->load->view('tops/home');
$this->load->view($content); $content is the $data['content'] from above
$this->load->view('bottoms/main_home');

Then the content section (home/home_content) would have a place to receive the $data[blog]

foreach (**$blog** as $row){ //controller main

$row->title = ucwords($row->title); //more code goes below
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜