4

I am currently develop a website application in codeigniter, and I'd like to do something in PHP / CodeIgniter where I can make a common template for separate sections of the website. I was thinking that I would keep the header / footer in a separate php files & include them separately.

The thing I'm not sure about is the content beneath the header and above the footer. This website application will contain a lot of different pages, so I'm having a hard time figuring how what's the best way to do this.

JaPerk14
  • 141

4 Answers4

3

I generally prefer to create a "layout" file rather than having to include both a header and footer on every page. It's more flexible.

Here's a snippet from one of my projects:

ob_start();
include '../views/'.$templateFile;
$pageContent = ob_get_clean();
include '../views/layouts/'.$layoutFile;

All you have to do is enable output buffering, include the template, then call ob_get_clean() to nab the contents of your template and put it into a variable. Once it's in a variable you can include your main layout file, which should echo $pageLayout somewhere inside.

e.g.,

<html>
<head>
    <title>Your Site</title>
</head>
<body>
    <!-- header here -->
    <?= $pageContents ?>
    <!-- footer here -->
</body>
</html>

That said, surely Code Igniter has some kind of templating built in, no? I'm not familiar with it. Edit: Apparently it does not. Kind of silly really; with caching, the performance cost of a nice templating engine is negligible.

mpen
  • 1,889
1

You can use Hook , or giving the parameter in every your method. Example :

Class My_controller extends CI_Controller{

    public function index(){
        $data = array(
            'container' => 'contain'
        );

        $this->load->view('base_view', $data);
    }

}

and also give your base_view.php (as a main view) the value from your sended variable.

<html>
<head>
    <title>Your title here</title>
</head>
<body>
    <header></header>

    <section id="container">
        <?php $this->load->view($container); ?>
    </section>

    <footer></footer>
</body>

Hope that helps..

1

Something I use most of the time, is just three (or more) views in the controller, like this:

class Mycontroller extends CI_Controller {
    public function index() {
        $headerData['title'] = 'Title of the page';
        $yourData['content'] = 'Everything you want to do inside your controller';

        $this->load->view('template/header',$headerData);
        $this->load->view('template/page',$yourData);
        $this->load->view('template/footer');
    }
}

This way you can also set, for example, a title in your header file.

edit: you can extend this with, for example, menu views or something to keep things even more seperated. Just make sure that you load the views in correct order.

trizz
  • 157
  • 7
0

I always used to create systems with CodeIgniter and this template library: http://www.williamsconcepts.com/ci/codeigniter/libraries/template/

It's very simple to use and it can do a better job than yours used techniques like using only $this->load->view() method.

Andrius
  • 161