views:

241

answers:

4

What is the best way to use controllers/views in CodeIgniter?

Is the following a bad way to go about making my pages?

function index()
{
    $this->load->library('carabiner');      
    $this->load->view('include/head');
    $this->load->view('include/home');
    $this->load->view('top');
    $this->load->view('featured');
    $this->load->view('eventslist');
    $this->load->view('footer');    
}

function create()
{
    $this->load->view('include/head');
    $this->load->view('top');
    $this->load->view('page');
    $this->load->view('dynamicstuff');
    $this->load->view('footer');
}

I'm quasi-templating this way, and it works fine for getting the outline of the site, but is it the best way to create an app? I've been working in CodeIgniter for about a week now, and I'm new to PHP.

+3  A: 

I would load only one view per function:

function index()
{
        $this->load->library('carabiner');              
        $this->load->view('index');
}

function create()
{
        $this->load->view('create');
}

Then include the other template files e.g. for index.php:

<?php 
    include 'include/head';
    include 'include/home';
    include 'top';
    include 'featured';
    include 'eventslist';
    include 'footer';
?>

Of course, you could group these views themselves and then include one view which itself includes header and navigation etc.

Residuum
Does this improve performance?
Kevin Brown
Performance of the website? I don't think so. Performance of programming? Definitely, as you will have a seperation of controllers and views and - executed correctly - between layout and content.
Residuum
Yeah this is a good model. Your controller shouldn't have to know all about headers and footers and that the featured thing goes ahead of the events list in the markup. That's all the view's job.
GloryFish
Ahh, that's helpful. I'm learning to change the way I think about this stuff--I'm totally self-taught...so HTML thinking patterns don't follow much at all for this stuff. :)
Kevin Brown
+1  A: 

There is a download called CodeIgniter Library that makes all this very easy. Once you learn how to implement it, coding becomes very easy. It also allows you to change each page or controller to have different templates (layouts).

Using the system I mentioned above you can do the following:

<?php

    function index() {

        $data['some_variable'] = 'some data';

        $this->template->write_view('content', 'page/home', $data);
        $this->template->render();

    }

?>

Anywhere you can also change which template you wish to use:

<?php

    $this->template->set_template('login');

?>

All these templates have names that are kept in a configuration file. You can have as many templates as you wish.

The template file itself would look something like this:

<html>
.... etc. all other html elements
    <body>
        header html goes here

        <?=$content?>

        footer html goes here
    </body>
</html>

You can even set up sections to be able to write your content to. I don't usually do this because it is including a lot of views, but if you need full control like this you are still able to do so:

<?php

    function index() {

        $data['header'] = 'header info';
        $data['content'] = 'content info';
        $data['sidebar'] = 'sidebar info';
        $data['footer'] = 'footer info';

        $this->template->write_view('content', 'page/home', $data);
        $this->template->write_view('header', 'modules/header');
        $this->template->write_view('sidebar', 'modules/sidebar');
        $this->template->write_view('footer', 'modules/footer');

        $this->template->render();

    }

?>

HTML template code:

<html>
.... etc. all other html elements
    <body>
        <div id="header">
            <?=$header?>
        </div>

        <div id="content">
            <div id="left">
                <?=$content?>
            </div>

            <div id="sidebar">
                <?=$sidebar?>
            </div>
        </div>

        <div id="footer">
            <?=$footer?>
        </div>
    </body>
</html>

This way you only have to worry about including one file in all your Controller Functions. If you are not using PHP 5 (you should switch) then instead of using <?=$content?> you will want to use <?php echo $content; ?>.

Will Ayers
+2  A: 

In all my controllers, I keep an array that I call $page_data. This holds all the stuff that I need to pass off to my view: page title, resources (js/css) to load, meta description, and content.

It looks something like this:

 function index(){
   $page_data['title'        ] = 'My Page Title';
   $page_data['description'  ] = 'All about my great page.';
   $page_data['content_file' ] = 'pages/index';
   $page_data['content_data']['books'] = $this->Books_model->get_books();
   $this->load->view('page-template', $page_data);
 }

Then under views, I have 'page-template.php', 'header.php', 'nav.php', 'footer.php' and 'pages/index.php'. All would be basic HTML/PHP, with the page-template.php looking something like this:

...
<title><?php echo $title ; ?></title>
....
<body>
  <?php $this->load->view('header'); ?>
  <?php $this->load->view('nav'); ?>
  <?php $this->load->view( $content_file, $content_data ); ?>
  <?php $this->load->view( 'footer'); ?>
</body>

Following that same pattern, you may find it handy to have a 'partial-template.php' or maybe 'json-template.php' for ajax/data feed pages.

This model proves to be very flexible as your sites grow.

rooskie
A: 

My Solution.

function index() 
{
   $data['main'] = 'content_view';

   $this->load->vars($data);
   $this->load->view('template');
}

Then... template.php

<html>
<body>
<div class="header">.... blah...</div>
<div class="main"><?php $this->load->view($main); ?></div>
<div class="footer">.... (c)2009, etc.</div>

I've never seen a reason to factor out the header and footer view.

Zack
I take those out because the areas above and below the main content will someimes change outside the index
Kevin Brown
Right... Which is why you load the main view... I'm just how many times is the 'header' different on different pages? Then it's not really a header any more, it's part of the content of that page.
Zack