2

I want to create common header and footer that are included on several view pages in MVC.

I'd like to use codeigniter for my project. Most people suggested me to use require_once php function in ci. But, how should I do?

Can Some one give a Step By Step Procedure to include require once in a view file. Through Controller, we can use like the following:-

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

How can we do it in view pages in ci using require_once..?

Deepak Keynes
  • 2,291
  • 5
  • 27
  • 56

2 Answers2

0

Controller

public function home(){
    $this->load->view('index');
    }
   public function about(){
    $this->load->view('about');
    }

Index View

$this->load->view('templates/header');

// Code of index file

$this->load->view('templates/footer');
Gopal Bhuva
  • 654
  • 2
  • 13
  • 20
0

After reading your question,I think you want to make Master Page or Layout with base style that will be contain Menu, footer and etc.

lets assume you have an html page

<html>
    <head>
        <title> Hello World </title>
    </head>
    <body>
        <div id="menu">
            <li><a href="#">Home</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Contact</a></li>
        </div>

        <div id="main-content">
            <!-- this is the dynamic part -->
        </div>

        <div id="footer">
            Copy Right 2013 Hello World
        </div>
    </body>
</html>

you could split it into 1- header 2- menu 3- main content 4- footer

you basically put

<html>
    <head>
        <title> Hello World </title>
    </head>
    <body>
in one view called "view_header" then you put

        <div id="menu">
            <li><a href="#">Home</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Contact</a></li>
        </div>
        <div id="main-content">
in a view called "view_menu" and then you put

        </div>

        <div id="footer">
            Copy Right 2013 Hello World
        </div>
    </body>
</html> 

in a view called "view_footer" then in your controller

$this->load->view('view_header');
$this->load->view('view_menu');
$this->load->view('YOUR_VIEW');
$this->load->view('view_footer');

The other solution, which I see is better: create a view called view_template_1.php

<html>
    <head>
        <title> Hello World </title>
    </head>
    <body>
        <div id="menu">
            <li><a href="#">Home</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Contact</a></li>
        </div>

        <div id="main-content">
            <?php $this->load->view($content); ?>
        </div>

        <div id="footer">
            Copy Right 2013 Hello World
        </div>
    </body>
</html>

in the controller lets say you want to call a view called About

$data = array('content'=>'about');
$this->load->view('view_template',$data);

Reference

Community
  • 1
  • 1
Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59