Codeigniter multiple views in one view

2019-04-07 21:28发布

问题:

I am working on a web application. This might be a silly question, but I want to know whether I am taking the good approach to this or not. I want to have multiple views on one single view/page.

The Codeigniter documentation says that "A view is simply a web page, or a page fragment, like a header, footer, sidebar, ...".

I want to have a header, some quick search view, some other view and a footer for a example. Should I implement a controller to every view (header, quick search, footer, ...), or is it better to implement every view functions in a single controller? For instance, if I know that the header, footer, quick search views are going to always be there (even if their content might change) should I put functions for all those views in one controller?

Please, help.

回答1:

one approach is to have a template view that has the elements you require. see this pseudo-code example... so, template_view.php has:

$this->load->view('header',$header);    
$this->load->view('quicksearch',$quickssearch);    
$this->load->view('body',$body);    
$this->load->view('footer',$footer);

your single controller then calls the template with the parameters for each view.

$data = new stdClass();
$data->header = ....
$data->quickssearch = ....
$data->body = .....
$data->footer = .....

$this->load->view('template_view',$data);