CodeIgniter Forums
Change the main content per controller change - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: Best Practices (https://forum.codeigniter.com/forum-12.html)
+--- Thread: Change the main content per controller change (/thread-68571.html)



Change the main content per controller change - nullstrike - 07-29-2017

I've been making school projects and I need ideas on how it can be done
I have 2 view files that is always included when making controller methods

I create a function where the main content will load based on the parameter supplied

Here's the code:
Code:
public function nextPage($link){
 $this->load->view('inc/header_sidebar');
 $this->load->view('pages/' . $link);
 $this->load->view('inc/footer_sidebar');
}

Then using the method like this:
Code:
public function user_index(){

$this->nextPage('user_view');
}

public function patient_index(){
$this->nextPage('patient_index');
}

I've been thinking that it is not a good approach since I think the header and footer will also load upon loading the said section


RE: Change the main content per controller change - Wouter60 - 07-29-2017

Make a MY_Controller (in application/core).
Create a method in it that renders the pages, the way you want.

PHP Code:
class MY_Controller extends CI_Controller {

    public 
$data = array();

    function 
__construct()
    {
        
parent::__construct();
    }

    protected function 
render_page($view)
    {
        
$this->load->view('inc/header_sidebar');
        
$this->load->view($view,$this->data);
        
$this->load->view('inc/footer_sidebar');
    }    



Let all your controllers extend this MY_Controller.
If you want to load a certain page with the default header and footer, just use $this->render_page('viewfile-name');
All the data that you want to pass to the view, can be added to the $this->data array, like:
PHP Code:
$this->data['records'] = $this->sample_model->get_all(); 



RE: Change the main content per controller change - skunkbad - 07-29-2017

This is how I do it:


PHP Code:
public function foo()
{
 
   // Passing along vars to the view when necessary
 
   $view_data NULL;

 
   $data = [
 
       // $content is in the main template
 
       'content' => $this->load->view('foo/bar'$view_dataTRUE )
 
   ];

 
   // Main template has the complete page (header, footer, everything) in it
 
   $this->load->view('templates/main'$data);




RE: Change the main content per controller change - nullstrike - 07-29-2017

I forgot to mention that the method is in MY_Controller.
I didn't try yet those ways you provide guys but would it accomplish what I want to achieve.

Like this, assumed I have a dashboard and when the index page is loaded all resources like jquery would also be loaded. Then when navigating to links inside the dashboard only the content wrapper that will change and it will not load the resources again.


RE: Change the main content per controller change - Wouter60 - 07-30-2017

Quote:assumed I have a dashboard and when the index page is loaded all resources like jquery would also be loaded. Then when navigating to links inside the dashboard only the content wrapper that will change and it will not load the resources again.

Nice thought, but it would mean that all content within the dashboard is shown via one URL. Once you change the URL (e.g. because you switch to another method inside the controller), a new page is loaded.
If you just want to change some elements on your page without refreshing the whole page, you will need AJAX.


RE: Change the main content per controller change - nullstrike - 07-30-2017

Okay, I'll try it. Could you show me some guide how to load the content