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.

  • i've been in this road , do yourself a favor and use a template engine ... i recommend blade ( laravel engine ) you can find a CI version on github – max Jan 5 '16 at 13:36
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.

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..

  • don't forget to create contain.php in your "view" folder to be loaded – Aldi Unanto Jun 28 '13 at 2:20
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.

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.

  • 1
    Your answer doesn't address all of the OP's concerns, especially with respect to the content between the header and footer. Your answer would be stronger if you explained why the linked template helps address the OP's concerns. – user53019 Jun 28 '13 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.