One-time include of global view

Sorry if this has been answered previously, I wasn't able to find anything and I'm brand new to CI.

I want to create one view that is my html header, doctype, etc. and auto display that over every page. Originally, I thought I could use hooks and set a view of my html header, then include the view that corresponded to the arguments (which is really just all the html inside <body>, then include some html footer at the end of it. When I did this the main view that got called via the URI overwrites the html header and then the view for the footer overwrites the URI's view. Can someone point me in the right direction?

Thank you.

[eluser]Michael Wales[/eluser]
The way you were doing it can work - you just need to review the Loader class and tell the view() method to assign the output to a variable rather than immediately echoing it (the view method's default action).

Another way: create a BaseController class that extends Controller, then make all of your controllers extend BaseController and rock some inheritance.

Hey there - thank you for the advice. I started writing a new BaseController to set some custom view methods to stack everything onto a var to echo at the end. I began crossing over into how the output actually works also. I found in there the following:

function set_output($output)
        $this->final_output = $output;

I modified it as:

function set_output($output)
        $this->final_output .= $output;

Basically just concatenating onto the final var there. This acheived exactly what I was looking for right off the bat with only this edit. Being that this is my first day, I'm wondering if you think this is a bad idea? I don't like the idea of modifying the core lib, but it seems like creating new classes is less congruent and more work than this simple edit. Thanks again.

Unless I'm missing something here, I just like to have:
<? $this->load->view('header'); ?>
<? $this->load->view('footer'); ?>
At the tops and bottoms of my views... though personally I include them both just inside the <body> tags so that the main views are properly formed HTML pages. Elements like doctype, etc... can be easily swapped out with a global search/replace if need be...
    &lt;title&gt;My Title&lt;/title&gt;
    &lt;link type="text/css" rel="stylesheet" media="all" href="&lt;?= base_url() ?&gt;assets/css/style.css"&gt;
    &lt;meta http-equiv="Content-Type" content="text/html;charset=utf-8" &gt;      
    &lt;? $this->load->view('header'); ?&gt;
    Content goes here....
    &lt;? $this->load->view('footer'); ?&gt;

[eluser]Michael Wales[/eluser]
You can do that (and that is how I do it as well) but you have to do that in every single one of your views. If you know before-hand that every single one of your pages will have a header and footer, extending a BaseController is a great way to accomplish this (as well as offer a lot control to you as well, want authentication across your entire site - add a single line, etc.)

In CI, as in life, there is always more than one way to skin a cat.

[eluser]Roger Glenn[/eluser]
Hi Walesmd,

Thanks for all your contributions to the forums. Lots of good stuff from your side.

I'd be curious to see your BaseController, if you don't mind sharing :-)

I'm considering a Page controller which all of my other controllers will extend. Page would set the header, footer and some "global" values. Maybe some other stuff too.

I'm also using an early version of CoolFactor's View library (the one that just sets variables and parts and loads a view).

Hello.... I'm not Walesmd ^^, but I do something like this:


class BaseController extends Controller {
    var $globalArray;
    function BaseController()
    function globalVars()
        $this->globalArray =
                                    'var1' => 'something',
                                    'var2' => 'something',
                                    'var3' => 'something'


And any controller:

class AnotherController extends BaseController {

    function AnotherController()

    // ...

And merge $globalArray of BaseController to array of any other controller to pass to view:
$newArray = array_merge($vars, $this->globalArray);

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.