Kamil
Kamil

Reputation: 13931

How to load page views like header, sidebar, footer in CodeIgniter?

Im trying to create my second web application in CodeIgniter.

In my previous project I created views for page header, footer and sidebar.

In every page controller I had to load these views like this:

class Home extends CI_Controller {

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

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

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

I don't like this and I feel im doing it wrong.

Any suggestions?

Upvotes: 3

Views: 38216

Answers (6)

Haris
Haris

Reputation: 81

Place all of them in a file like 
main.php

And load it on top of view rather than always sending from controller.

main.php contain 
<php 
$this->load->view('header');
$this->load->view('sidebar');
$this->load->view('home');
$this->load->view('footer');
?>

and in view just place

<php 
$this->load->view('main');
?>

at top of file

Upvotes: -1

Shailender Ahuja
Shailender Ahuja

Reputation: 312

The best way is to add these files in constructors

function __construct() { 
     parent::__construct(); 
        $this->load->helper('url'); 
        $this->load->database();
        $this->load->view('prots/header');
        $this->load->view('prots/top');
        $this->load->view('prots/navigation');
  } 

Upvotes: 0

Majid Golshadi
Majid Golshadi

Reputation: 2706

in advance, you can through this way to build your view:

first create an Template in your view folder

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <meta content="MajidGolshadi" name="author">
    <?php echo $html_head; ?>
    <title></title>
</head>
<body>
    <div id="content">
            <?php echo $content; ?>
    </div>

    <div id="footer">
        <?php echo $html_footer; ?>
    </div>
</body>
</html>

second create an library to load your view automaticaly

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Template
{
    private $data;
    private $js_file;
    private $css_file;
    private $CI;

    public function __construct()
    {
        $this->CI =& get_instance();
        $this->CI->load->helper('url');

        // default CSS and JS that they must be load in any pages

        $this->addJS( base_url('assets/js/jquery-1.7.1.min.js') );
        $this->addCSS( base_url('assets/css/semantic.min.css') );
    }

    public function show( $folder, $page, $data=null, $menu=true )
    {
        if ( ! file_exists('application/views/'.$folder.'/'.$page.'.php' ) )
        {
            show_404();
        }
        else
        {
            $this->data['page_var'] = $data;
            $this->load_JS_and_css();
            $this->init_menu();

            if ($menu)
                $this->data['content'] = $this->CI->load->view('template/menu.php', $this->data, true);
            else
                $this->data['content'] = '';

            $this->data['content'] .= $this->CI->load->view($folder.'/'.$page.'.php', $this->data, true);
            $this->CI->load->view('template.php', $this->data);
        }
    }

    public function addJS( $name )
    {
        $js = new stdClass();
        $js->file = $name;
        $this->js_file[] = $js;
    }

    public function addCSS( $name )
    {
        $css = new stdClass();
        $css->file = $name;
        $this->css_file[] = $css;
    }

    private function load_JS_and_css()
    {
        $this->data['html_head'] = '';

        if ( $this->css_file )
        {
            foreach( $this->css_file as $css )
            {
                $this->data['html_head'] .= "<link rel='stylesheet' type='text/css' href=".$css->file.">". "\n";
            }
        }

        if ( $this->js_file )
        {
            foreach( $this->js_file as $js )
            {
                $this->data['html_head'] .= "<script type='text/javascript' src=".$js->file."></script>". "\n";
            }
        }
    }

    private function init_menu()
    {        
      // your code to init menus
      // it's a sample code you can init some other part of your page
    }
}

third load your library in every controller constructor and then use this lib to load you view automatically

to load additional js in your view you can use addJS method in this way:

$this->template->addJS("your_js_address");

for Css:

$this->template->addCSS("your_css_address");

and to show your page content call your view file with show method

$this->template->show("your_view_folder", "view_file_name", $data);

i hope this codes help you

Upvotes: 8

Mudshark
Mudshark

Reputation: 3253

You could create a library (or helper) like so:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class View_lib{

   function __construct(){
    $this->CI =& get_instance();
   }


   function load_view($current_page){
       $parts = array('header', 'sidebar', $current_page, 'footer');
       foreach($parts as $part){
       $this->CI->load->view($part);
    }
   }    
}

Controller:

public function index(){
    $this->load->library('view_lib'); // probably better autoload this
    $this->view_lib->load_view('about');
}

Kind of barebones, but you get the idea...

Upvotes: 1

Dexter
Dexter

Reputation: 1796

create template.php in view put this code into this

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

in controller use this code

public function index()
    {
        $data['middle'] = 'home';
    $this->load->view('template',$data);
    }

Upvotes: 3

Rooneyl
Rooneyl

Reputation: 7902

You could load the header,sidebar, footer in a main template file that you use.

// contact.html
<?php $this->load->view('header');?>
<p>Yak yak yak</p>
<?php $this->load->view('footer');?> 

Or use a template library like this one

Upvotes: 2

Related Questions