CodeIgniter Forums
Including a file - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Including a file (/showthread.php?tid=34782)



Including a file - El Forum - 10-09-2010

[eluser]sahanlak[/eluser]
Does codeigniter have any functions for file including? I have a file that shows a sidebar through out the entire site, it has some database results showing so I was hoping to include it to pages. I don't like to do this from load->view since it breaks the mvc pattern (don't know if its possible Smile ) What should I do? Create a single page for a sidebar within a controller and include it?


Including a file - El Forum - 10-09-2010

[eluser]ram4nd[/eluser]
use helper or library


Including a file - El Forum - 10-09-2010

[eluser]cahva[/eluser]
The best would be to use base controller which you can use to set things up. Heres a little tutorial how to use base controllers:
http://philsturgeon.co.uk/news/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY

Heres an overly simplified example:

Front_controller (base controller that you will extend from in your normal controllers)
Code:
class Front_controller extends Controller {

    var $data = array(
        'sidebar' => '',
        'content' => ''
    );

    function __construct()
    {
        parent::__construct();
        
        // Load models and libraries you'll need for the sidebar here
        $this->load->model('pages_model');
        
        $this->data['sidebar'] = $this->load->view('sidebar',array('links' => $this->pages_model->get_links()),TRUE);
    }

}
EDIT: sidebar view would be partial view and in this case something that will have a foreach or similar to iterate links.

Somepage controller:
Code:
class Somepage extends Front_controller {

    function __construct()
    {
        parent::__construct();
    }
    
    function index()
    {
        // insert content to be used
        $this->data['content'] = $this->load->view('somepage','',TRUE);

        // Load layout
        $this->load->view('layout',$this->data);
    }
}

View for layout
Code:
<div id="sidebar">
    &lt;?=$sidebar ?&gt;
</div>

<div id="content">
    &lt;?=$content ?&gt;
</div>

EDIT: Had some errors in $this->load->view. Forgot to use the third parameter TRUE to returns the view. Fixed now.


Including a file - El Forum - 10-09-2010

[eluser]sahanlak[/eluser]
[quote author="cahva" date="1286657542"]The best would be to use base controller which you can use to set things up. Heres a little tutorial how to use base controllers:
http://philsturgeon.co.uk/news/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY

Heres an overly simplified example:

Front_controller (base controller that you will extend from in your normal controllers)
Code:
class Front_controller extends Controller {

    var $data = array(
        'sidebar' => '',
        'content' => ''
    );

    function __construct()
    {
        parent::__construct();
        
        // Load models and libraries you'll need for the sidebar here
        $this->load->model('pages_model');
        
        $this->data['sidebar'] = $this->load->view('sidebar',array('links' => $this->pages_model->get_links()));
    }

}
EDIT: sidebar view would be partial view and in this case something that will have a foreach or similar to iterate links.

Somepage controller:
Code:
class Somepage extends Front_controller {

    function __construct()
    {
        parent::__construct();
    }
    
    function index()
    {
        // insert content to be used
        $this->data['content'] = $this->load->view('somepage');

        // Load layout
        $this->load->view('layout',$this->data);
    }
}

View for somepage
Code:
<div id="sidebar">
    &lt;?=$sidebar ?&gt;
</div>

<div id="content">
    &lt;?=$content ?&gt;
</div>
[/quote]

Thanks a lot Smile


Including a file - El Forum - 10-09-2010

[eluser]cahva[/eluser]
Corrected some errors in my code. The last view was for the "layout", not "somepage". Also didnt return data with load->view with the partials. Corrected now.