Welcome Guest, Not a member yet? Register   Sign In
automate loading header and footer views
#1

[eluser]eilrahc[/eluser]
Hiya,

I'm brand-new to codeigniter and so far I'm pretty impressed. I'm setting up a toy application to start with and finally ran into something the docs don't seem to address.

It's a basic CRUD-style app and all of the pages will look roughly the same, so I thought I'd divide the views up between a header, body, and footer. In the docs for the Views, it says you can load multiple views as in:

Code:
$this->load->view('header');
$this->load->view('body');
$this->load->view('footer');

This is great but I was wondering if there's a way to have the header and footer automatically loaded so I don't have to add the header and footer lines to every controller function. The header view displays just fine on each page when loaded from the constructor, so I thought that maybe a destructor would do the same for the footer. Unfortunately, it doesn't. I suspect that by the time the destructor is called, codeigniter has wrapped up all the view stuff or something.

Is there any way (or a better way) to have both my header and footer views loaded automatically without explicitly loading them in each function?
#2

[eluser]Adam Owen[/eluser]
I think it's best just to include them in your view files.

Code:
<?php $this->load->view('header'); ?>

Stuff goes here...

<?php $this->load->view('footer'); ?>

It keeps your controllers cleaner and I don't think it's worth the hassle to try and get it more automated than that.
#3

[eluser]Pascal Kriete[/eluser]
::Note to self, if you type a lot - refresh::

Hey eilrahc,

There is a way to do this with a destructor, but it's not very sexy so I'll introduce you to two pretty common methods.

Template View
You can nest view loading calls, so you could create a main template:
Code:
<?php $this->load->view('header'); ?>
<div id="content">

&lt;?php $this->load->view($content); ?&gt;

</div>
&lt;?php $this->load->view('footer'); ?&gt;

And then load that every time, passing in the name of your current view:
Code:
$data['content'] = 'myviewpath';
$this->load->view('template', $data);

Template Library
Make a library to do the task for you.
Code:
class Template {
    function show($view)
    {
        &lt;?php $this->load->view('header'); ?&gt;
        &lt;?php $this->load->view($view); ?&gt;
        &lt;?php $this->load->view('footer'); ?&gt;
    }
}
You could autoload that, and then in any point of your app do $this->template->show('my/view/path')

My preferred method is somewhere inbetween. You just find something you're comfortable with.

Welcome to CodeIgniter!
#4

[eluser]Colin Williams[/eluser]
I've released a Library that helps with this pattern. Follow the link in my signature below. Using my Template class, you would basically do this in a Controller constructor, or in some custom bootstrapping routine, like in a hook:

Code:
$this->template->write_view('header', 'common/header');
$this->template->write_view('footer', 'common/footer');

Although, if your header and footer views are not dynamic (they don't rely on variables being passed in) then you just include their code in the master template. The Template Library link below has full documentation, so give it a read and try it out if it looks good to you.
#5

[eluser]eilrahc[/eluser]
Thanks for the great suggestions, and in just a few hours too! I'll be sure try them out.
#6

[eluser]eilrahc[/eluser]
Just wanted to send along an update on this. I finally had a chance to play with Colin's Template Library and it's exactly what I was looking for, even in my little toy app. Why on earth doesn't this ship with CodeIgniter in the first place?
#7

[eluser]Colin Williams[/eluser]
Good to hear, eilrahc. Good to hear. I'll be releasing a critical update tonight that fixes fatal errors caused by the add_js() and add_css() methods.
#8

[eluser]Jilani Jidni[/eluser]
You can also try this.
Code:
$data['mainContent'] = $this->load->view('page_name', $data, true);
$data['homeTitle'] = 'page Title';
$this->load->view('Template_page', $data);

and template should me look like this

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;&lt;?=$homeTitle?&gt;&lt;/title&gt;

&lt;/head&gt;

&lt;body&gt;
    <div id="container">
        <div>Header</div>
        <div>&lt;?=$mainContent?&gt;</div>
        <div>Footer</div>
    </div>
&lt;/body&gt;
&lt;/html&gt;
#9

[eluser]webdevguy32[/eluser]
Jilani - that is an elegant solution




Theme © iAndrew 2016 - Forum software by © MyBB