Skip to content

Header and Footer and Menu on every page jedd

World Wide Web Server edited this page Jul 4, 2012 · 11 revisions

Category:Approaches::Header and Footer and Menu on every page

[b]Overview:[/b]

I have three things that I want to see on pretty much every page: [b]o[/b] header - page title, login status, and maybe some links [b]o[/b] footer - links, page rendering stats, and some advertising back to the CI site! [b]o[/b] menu - generated from an array (not DB) containing only category and sub-category levels

Because I want to see them pretty much on every page, and I consider the process to be inexpensive, I am happy to have them created on [i]every[/i] page load.

If you don't want this - you could modify this approach by putting some smarts into MY_Controller to identify whether to create the vars or not - but this might be messier than other Approaches to the problem.

[b]Approach (brief):[/b]

I [url="http://codeigniter.com/user_guide/general/core_classes.html"]extend the core Controller[/url] and make [b]MY_Controller.php[/b].

Within MY_Controller I call out to view files that generate sub-sets, typically

's or 's, and perhaps a few dozen lines of HTML. These are called using the [b]load->view('view', $data, [color=red]TRUE[/color])[/b] feature - described in detail at the [url="http://codeigniter.com/user_guide/general/views.html"]very bottom of the Views[/url] page in the CI User Guide.

These view outputs are stored into variables within the [b]$this->data[/b] array. The primary view file then just displays these data using simple echo statements.

[b]Approach (in detail):[/b]

Let's work backwards from the end result - I think this will make more sense.

This is roughly what my [color=green][b]primary view file (default.php)[/b][/color] looks like. Note that this is what [b]all[/b] my pages are rendered through.

[code] <html> <head> <?php echo "\n". link_tag('assets/stylesheets/COMMON.css'); echo "\n". link_tag('assets/stylesheets/'. $theme .'.css'); ?>

<title> <?php echo $title; ?> </title> </head>

<body>

<?php ?>

<div id="div_topbar_wrapper">
    &lt;?php echo $top_bar_view; ?&gt;
</div>

<div class="break">
</div>

<div id="div_torso_wrapper">

    <div id="div_navigation_menu">
        &lt;?php echo $main_menu_view; ?&gt;
    </div>

    <div id="div_main_content">
        &lt;?php echo $main_content_view;  ?&gt;
    </div>

</div>

<div class="break">
</div>

<div id="div_footer_wrapper">
    <table width="100%">
        <tr width="100%">
        <td width="25%" align="left">
            <b>
            &lt;!-- Can't pre-render this, as it'll throw the reported results --&gt;
            Rendered in {elapsed_time}s, and {memory_usage}.
            </b>
        </td>
        <td width="50%" align="center">
            $other_stuff
        </td>
        <td width="25%" align="right">
            Developed using
            &lt;?php echo anchor_popup ("http://codeigniter.com/", "Code Igniter"); ?&gt;
        </td>
        </tr>
    </table>
</div>

</body> </html> [/code]

Now let's look at what goes into one of those view snippets that I'm echo'ing there. My top view contains three divs, generated separately, so not a good candidate example. My menu is lengthy, but most people understand menus, and I happen to generate a slightly different menu if the logged in user is [b]admin-equiv[/b], so it's probably a quite instructive choice.

Clone this wiki locally