Welcome Guest, Not a member yet? Register   Sign In
Override $this->load->view() behaviour with a DRY view template approach.
#1

[eluser]Denarius[/eluser]
Save as my_loader.php in application/libraries if you'd like to develop with views based on a template approach, but without having to
nest views. I took an override approach just because I prefer this as a default approach with a wrapper method for the parent
included for unusual circumstances. I've been playing with code igniter for a week and I'm really enjoying the whole framework approach. I can see it will save me quite a lot of time compared to always scripting from scratch!

My first stab at extending a library class so I'm sure it can be improved upon.

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

class MY_Loader extends CI_Loader {
/*
Place the class in application/libraries for your CodeIgniter application. This
plugin overrides the default $this->load->view() behaviour. Create a folder
called shared_sections in the views folder and create files called head.php,
header.php, navigation.php and footer.php within it. Your views will only need to
contain your content div and your controllers will not need to be modified since
$this->load->view is now triggering the method on MY_Loader instead of CI_Loader.

Set $this->load->no_header=TRUE before the $this->load->view() call in your
controller to suppress loading of the header div. Similarly, set
$this->load->no_navigation=TRUE to suppress loading of the navigation div.
*/
//Variable suppresses loading of the header div file.
    var $no_header = FALSE;
//Variable suppresses loading of the navigation div file.
    var $no_navigation = FALSE;
    
    function MY_Loader()
    {
        parent::CI_Loader();
    }
    
    function view($view, $vars = array(), $return = FALSE)
    {
//head.php contains xml declaration, doctype and the opening HTML tag and head element.
    $output=parent::view('shared_sections/head.php', $vars, TRUE);
//header.php contains a div for the page header/banner, this can be suppressed optionally.
    if (!$this->no_header) $output.=parent::view('shared_sections/header.php', $vars, TRUE);
//Your navigation menu, optionally suppressed.
    if (!$this->no_navigation) $output.=parent::view('shared_sections/navigation.php', $vars, TRUE);
//this is the non-repeating part of the view loaded as dictated in your controller.
  $output.=parent::view($view, $vars, TRUE);
    $output.=parent::view('shared_sections/footer.php', $vars, TRUE);
    if ($return == FALSE) echo $output;
    else return $output;
    }
//This just calls the overridden load->view() method if there is a need for it.
   function basic_view($view, $vars = array(), $return = FALSE)
   {
   parent::view($view, $vars, $return);
   }
    
}

?>
#2

[eluser]Krynble[/eluser]
Hey Denarius, I just started using CodeIgniter and my very first action was to try creating a DRY template approach.

Your suggestion looks great, but at the same time, it brought me a problem. Our services are usually very requested, and we use caching to reduce processing time.

Unfortunately, this approach seemed to bug the way CodeIgniter does caching, for the content is not shown; it's returned instead. It's weird, I did a little searching and couldn't find any solutions until now.

I will update in case I find something new about it.

Best regards,
Omar Ajoue.
#3

[eluser]Denarius[/eluser]
I think the echo at the end combined with running the parent::view method only as true probably broke it. Here's another version trying a new tack with an if clause for the $return condition.

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

class MY_Loader extends CI_Loader {
/*
Place the class in application/libraries for your CodeIgniter application. This
plugin overrides the default $this->load->view() behaviour. Create a folder
called shared_sections in the views folder and create files called head.php,
header.php, navigation.php and footer.php within it. Your views will only need to
contain your content div and your controllers will not need to be modified since
$this->load->view is now triggering the method on MY_Loader instead of CI_Loader.

Set $this->load->no_header=TRUE before the $this->load->view() call in your
controller to suppress loading of the header div. Similarly, set
$this->load->no_navigation=TRUE to suppress loading of the navigation div.
*/
//Variable suppresses loading of the header div file.
    var $no_header = FALSE;
//Variable suppresses loading of the navigation div file.
    var $no_navigation = FALSE;
    
    function MY_Loader()
    {
        parent::CI_Loader();
    }
    
    function view($view, $vars = array(), $return = FALSE)
    {
//check if return is true or false and load each view with false or load each with true, concatenate and return.
if(!$return)
{
//head.php contains xml declaration, doctype and the opening HTML tag and head element.
    parent::view('shared_sections/head.php', $vars, FALSE);
//header.php contains a div for the page header/banner, this can be suppressed optionally.
    if (!$this->no_header) parent::view('shared_sections/header.php', $vars, FALSE);
//Your navigation menu, optionally suppressed.
    if (!$this->no_navigation) parent::view('shared_sections/navigation.php', $vars, FALSE);
//this is the non-repeating part of the view loaded as dictated in your controller.
  parent::view($view, $vars, FALSE);
    parent::view('shared_sections/footer.php', $vars, FALSE);
  }
else
{
//head.php contains xml declaration, doctype and the opening HTML tag and head element.
    $output=parent::view('shared_sections/head.php', $vars, TRUE);
//header.php contains a div for the page header/banner, this can be suppressed optionally.
    if (!$this->no_header) $output.=parent::view('shared_sections/header.php', $vars, TRUE);
//Your navigation menu, optionally suppressed.
    if (!$this->no_navigation) $output.=parent::view('shared_sections/navigation.php', $vars, TRUE);
//this is the non-repeating part of the view loaded as dictated in your controller.
  $output.=parent::view($view, $vars, TRUE);
    $output.=parent::view('shared_sections/footer.php', $vars, TRUE);
  return $output;
  }

    }
//This just calls the overridden load->view() method if there is a need for it.
   function basic_view($view, $vars = array(), $return = FALSE)
   {
   parent::view($view, $vars, $return);
   }
    
}

?>

Quickly tried it and my output on screen, but haven't tested rigorously and certainly haven't looked at caching. Interested to hear how it goes.




Theme © iAndrew 2016 - Forum software by © MyBB