Rajura
Rajura

Reputation: 27

What's good way to load header and footer view in codeigniter?

I'm using codeigniter 3x. I'm working on my website. I'm using include method in my view. Like

<?php include('templates/header.php'); ?>
<h1>Home Page</h1>
<?php include('templates/footer.php'); ?>

Is this a good way to show header and footer in codeigniter. Thank!

Upvotes: 1

Views: 1488

Answers (1)

user969068
user969068

Reputation: 2943

You are half your way, here is how you will be able to make it more dynamic, in your views file you should have a structure like this:

views 
  - header.php
  - footer.php
  - template.php
  - home.page

In header.php you should have all your header and footer content which you wants to display on all pages.

Now in your template move all your includes.

template.php

<?php $this->load->view("header.php"); ?>
<?php $this->load->view($main_content); ?>
<?php $this->load->view("footer.php"); ?>

here you notice $main_content variable, it is dynamic file name which we want to load in our controller. So lets assume you have a controller like this:

public function home()
{
    $data['meta_title'] = $this->lang->line('home_meta_title');
    $data['meta_description'] = $this->lang->line('home_meta_description');
    $data['meta_keywords'] = $this->lang->line('home_meta_keywords');
    $data['main_content'] = 'home';
    $this->load->view('template',$data);
}

$data['main_content'] = 'home'; is loading your home.php file, you can also load from subdirectories like 'directory/home'. You can also pass any variable like I gave you above example with dynamic meta.

Upvotes: 3

Related Questions