• 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Add Dynamic Header and Footer

#1
Here is my code:

controllers/Home.php


PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Home extends CI_Controller {
    
    
public function __construct()
    {
        parent::__construct();
        $this->load->helper('url_helper');
    }
    
    
public function index()
    {
        if ( ! file_exists(APPPATH.'views/themes/default/templates/common/home.php'))
        {
            // Whoops, we don't have a page for that!
            show_404();
        }
        
        $data
['title'] = "Home";
        
        $this
->load->view('themes/default/templates/common/header'$data);
        $this->load->view('themes/default/templates/common/home'$data);
        $this->load->view('themes/default/templates/common/footer'$data);
    }



views/themes/default/templates/common/header.php

PHP Code:
<!DOCTYPE html>
<
html>
<
head>
<
title>Title</title>
</
head>
<
body>
<
header>
    <div class="container"><?php echo $title?></div>
</header> 


views/themes/default/templates/common/home.php

PHP Code:
<div class="container">
    <h1><?php echo $title?></h1>
</div> 


views/themes/default/templates/common/footer.php


PHP Code:
<footer>
    <div class="container"><?php echo $title?></div>
</footer>
</body></html> 


Right now, it shows the text Home in each section (in header, in home, and in footer).

I want each page shows different titles (header shows text Header, home shows text Home, and footer shows text Footer).

How to achieve it? Thanks.
Reply

#2
I solved this problem with MY_Controller.php in folder app/core/ I created a method/action with this:

PHP Code:
//...
public function theme($file){
 
   $this->load->view('themes/default/templates/common/header'$data);
 
   $this->load->view('themes/default/templates/common/'.$file$data);
 
   $this->load->view('themes/default/templates/common/footer'$data);
}
//... 

in controller extends the MY_Controller and call view with
PHP Code:
$this->theme('home'); 


see more about MY_Controller here: https://www.codeigniter.com/user_guide/g...asses.html
Reply

#3
(03-02-2018, 04:35 PM)wallacesilva09 Wrote: I solved this problem with MY_Controller.php in folder app/core/ I created a method/action with this:

PHP Code:
//...
public function theme($file){
 
   $this->load->view('themes/default/templates/common/header'$data);
 
   $this->load->view('themes/default/templates/common/'.$file$data);
 
   $this->load->view('themes/default/templates/common/footer'$data);
}
//... 

in controller extends the MY_Controller and call view with
PHP Code:
$this->theme('home'); 


see more about MY_Controller here: https://www.codeigniter.com/user_guide/g...asses.html

The $data, where it comes from?

If all views have the same $data inputs, then all views will display the same data, right? Just like in my case.
Reply

#4
You need to add different keys to your $data array.
PHP Code:
$data['header'] = 'Header';
$data['home'] = 'Home';
$data['footer'] = 'Footer';

<?
php echo $header?>
<?php 
echo $home?>
<?php 
echo $footer?>

PHP Code:
$data['title']['header'] = 'Header';
$data['title']['home'] = 'Home';
$data['title']['footer'] = 'Footer';

<?
php echo $title['header']; ?>
<?php 
echo $title['home']; ?>
<?php 
echo $title['footer']; ?>
Reply

#5
Hi,

I will show given below one simple code

//...
public function theme($file){
$this->load->view('themes/default/templates/common/header', $data);
$this->load->view('themes/default/templates/common/'.$file, $data);
$this->load->view('themes/default/templates/common/footer', $data);
}
//...

Hope that Helps!
Regards,
Daniel
Reply

#6
The solution is fairly simple:

PHP Code:
$header['title'] = "Header";
$data['title'] = "Home";
$footer['title'] = "Footer";

 
       $this->load->view('themes/default/templates/common/header'$header);
 
       $this->load->view('themes/default/templates/common/home'$data);
 
       $this->load->view('themes/default/templates/common/footer'$footer); 
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2018 MyBB Group.