When you are using codeigniter, you will use function $this->load->view to load your ‘view’. In codeigniter user guide, a view is described as simply a web page, or a page fragment, like a header, footer, sidebar, etc. In fact, views can flexibly be embedded within other views (within other views, etc., etc.) if you need this type of hierarchy.

In my last project, some fragment on view is loaded in every page. They are header, footer and sidebar. And the content is dynamic. I don’t want to load views for every page. So i write a function in a library (or you can also extends the controller) to handle loading views for every page.

public function create_page($page = NULL ,$data = array()){
        $defaults = array('header' => array('title' => 'Home'),
            'content'=> '',
            'sidebar' => '',
            'footer'=> '');

        foreach ($defaults as $key => $val) {
                    $$key = ( ! isset($data[$key])) ? $val : $data[$key];

        if(! isset($page)){

In my function above, dynamic page is stored under ‘page’ folder. See the picture



Using this function is so simple,

$data = array(
                    'header' => array('title' => 'Testing Title'),
                    'content'=> array('data_content' => 'dummy content'));

In the example above, i just send 2 variable to the view, $title for ‘header’ page and $data_content for ‘page_view’ page. In the ‘page_view’, i use the variables like this

echo $data_content;

any comments? CMIIW

One thought on “$this->load->view()

  1. chipz

    lha ini yang saya mau ndan! saya tidak kepikiran kalo penggunaan library bisa cuman sebagai by-pass saja, pemberian content datane tetap di controller (kalo mau dari database bisa diambil dari model juga tentunya) nice post ndan!



Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.