How to set template by using library in codeigniter

Codeigniter

Posted on 25 Aug 15 by Vickey
  1685



By using library we can set themes in a codeigniter app. In codeigniter by defualt we load header and footer in every action.

public function index()
{
    $this->load->view('header');
    $this->load->view('index');
    $this->load->view('footer');
}

But if we use a library for this we can make it easy. so lets create a easy theme for codeigniter

1.Go to /application/libraries directory and create a file 'Layout.php' and add the below code

    class Layout 
    {
        var $codeigniter;
        function __construct() 
        {
            $this->codeigniter =& get_instance();
        }
        
        function load($template, $page_content = null, $data = null) 
        {
            
            if ( ! is_null( $page_content )) 
            {
                
                if (file_exists(APPPATH.'views/'.$template.'/'.$page_content )) 
                {
                    $page_content_path = $template.'/'.$page_content;
                }
                else if (file_exists(APPPATH.'views/'.$template.'/'.$page_content.'.php' )) 
                {
                    $page_content_path = $template.'/'.$page_content.'.php';
                }
                else if (file_exists(APPPATH.'views/'.$page_content )) 
                {
                    $page_content_path = $page_content;
                }
                else if (file_exists(APPPATH.'views/'.$page_content.'.php' )) 
                {
                    $page_content_path = $page_content.'.php';
                }
                else
                {
                    show_error('Requested view can not be load: ' . $template.'/'.$page_content.'.php');
                }
                 
                $content = $this->codeigniter->load->view($page_content_path, $data, TRUE);
                 
                if (is_null($data)) 
                {
                    $data = array('content' => $content);
                }
                else if (is_array($data))
                {
                    $data['content'] = $content;
                }
                else if (is_object($data))
                {
                    $data->body = $content;
                }
            }
            $this->codeigniter->load->view('layouts/'.$template, $data);
            
        }
        
    }

2. go to directory /application/config and open file 'autoload.php' and here set your template library

$autoload['libraries'] = array('Layout','database');

it will load your layout library

3. Now go to /application/views and create two file 'header.php' and 'footer.php' and put your html code accordingly

4. Under application/views  create a new directory 'layouts' and create a file 'main.php' under this and add the below code in the file

<?php $this->load->view('header'); ?>
<?php echo $content; ?>
<?php $this->load->view('footer'); ?>

you have created your first theme you can use this by using the below code

public function index()
{
    $data = array('title' => 'Your title',);
    $this->layout->load('main', 'content', $data);
}

here content is respected view of this action and main is the template name


If this post help you reduce time to develop, you can help me to maintain this website :)
www.000webhost.com