Welcome Guest, Not a member yet? Register   Sign In
Default header and footer with loading a view
#1

[eluser]Francois Gelinas[/eluser]
Hi, I wrote this little extension to the Loader library to standardize my pages layout without having to include 2 files in each of my views (and make it easier to switch to a different layout)

This extension to the Loader library will add a header and a footer to each views, all data passed to the view will also be available to the header and footer views.

By default, the files will be loaded from the layout/default folder in the view folder.
ie: layout/default/header.php and layout/default/footer.php

To use a header and footer from a different layout folder, simply pass the name of the folder as the fourth parameter to the view() function. To load a view without layout, pass FALSE as the fourth parameter.

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
* Add layout system to load->view    (header & footer)
*
* This will embed the requested view with a header and footer file.
*
* By default, the header will be located at : layout/default/header.php
* and the footer at : layout/default/footer.php
*
* To replace the 'default' layout, pass the name as the fourth parameter to the view() function.
*
* To call without a layout, pass FALSE as the fourth parameter.
*
* By Francois Gelinas on june 2009
*
*/


class MY_Loader extends CI_Loader
{
    function MY_Loader()
    {
        parent::CI_Loader();
    }

    /*
     *  Override default view function to add layout (header & footer) functionality.
     *  To get a view without a layout, simply pass FALSE as the fourth parameter.
     *  To get a different layout, pass the name of of the folder as the fourth parameter.
     */
    function view($view, $vars = array(), $return = FALSE, $layout = 'default')
    {
        $return_value = '';

        // process header
        if ($layout)
        {
            $header_file = "layout/{$layout}/header.php";
            $return_value .= $this->_ci_load(array('_ci_view' => $header_file, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
        }
        // process requested view
        $return_value .= $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
        // process footer
        if ($layout)
        {
            $footer_file = "layout/{$layout}/footer.php";
            $return_value .= $this->_ci_load(array('_ci_view' => $footer_file, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
        }
        return $return_value;
    }
    
}
#2

[eluser]gtech[/eluser]
That is quite a cool solution I had not thought of. cheers.
#3

[eluser]Dam1an[/eluser]
I've done a similar thing by making a seperate library which I use instead of load->view as I needed to support having several layouts, but this (with a few tweaks) could be a much cleaner solution
Thanks for sharing
#4

[eluser]xwero[/eluser]
Instead of overwriting the view method i think you better create a new method, for example template, if another developer likes to use the CI view method he can.

Having to change the header and footer per site in the class is not developer friendly. You better add class variables to the extended class and get them for a config file. That way if some page has another footer or header it can be changed.
Code:
$this->load->set_header('some_header');
$this->load->template('actual_page_content');
#5

[eluser]Dark Preacher[/eluser]
I'm doing it like this:
in views folder file named "template.php"
Code:
$this->load->view('_header');
$this->load->view('_left');
$this->load->view($main);
$this->load->view('_footer');

and in controller
Code:
$data['main'] = 'catalog_root';
$this->load->view('template', $data);

Pretty simple, no hacks to the core and easy to use.
#6

[eluser]Iverson[/eluser]
[quote author="Dark Preacher" date="1245105678"]I'm doing it like this:
in views folder file named "template.php"
Code:
$this->load->view('_header');
$this->load->view('_left');
$this->load->view($main);
$this->load->view('_footer');

and in controller
Code:
$data['main'] = 'catalog_root';
$this->load->view('template', $data);

Pretty simple, no hacks to the core and easy to use.[/quote]

+1
#7

[eluser]Kamape[/eluser]
I've been taking your code Francois Gelinas and replaced it with a masterpage template instead.

Code:
function view($view, $vars = array(), $return = FALSE, $layout = 'default')
    {
        $return_value = '';

        // process requested view
        $return_value .= $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => TRUE));
        
        // process masterpage
        if ($layout)
        {
            $header_file = "layout/{$layout}.php";
            $template = $this->_ci_load(array('_ci_view' => $header_file, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => TRUE));
            $return_value = str_replace('{CONTENT}', $return_value, $template);
        }    
        
        if($return)
            return $return_value;
          
        echo $return_value;
    }

In this way I personally think it's easier to manage the header and footer when you're having it in the same file. Also when you want to work with the template you just choose not to include anything from the controller. But that's just the same as the original example. Just wanted to share my thoughts.

Great work of course. Thanks!
#8

[eluser]adamp1[/eluser]
[quote author="Iverson" date="1245350722"][quote author="Dark Preacher" date="1245105678"]I'm doing it like this:
in views folder file named "template.php"
Code:
$this->load->view('_header');
$this->load->view('_left');
$this->load->view($main);
$this->load->view('_footer');

and in controller
Code:
$data['main'] = 'catalog_root';
$this->load->view('template', $data);

Pretty simple, no hacks to the core and easy to use.[/quote]

+1[/quote]
+1, this the simplest and best tbh
#9

[eluser]Kamape[/eluser]
A matter of taste i'll think...

Code:
$this->load->view('my_view',$data);
$this->load->view('my_view',$data,FALSE,NULL);

In this case you can change the name of your default layout without breaking 200 controllers who all specifies the 'template' string.

Everyone has their own opinion
#10

[eluser]Francois Gelinas[/eluser]
Hey Kamape, good idea with the masterpage, I don't know why I wanted to store it all in 2 different files that much hehe.

Also, thanks everyone for sharing your ideas and methods.




Theme © iAndrew 2016 - Forum software by © MyBB