Codeigniter Mod – Builder

After finishing my work with the opencart platform for the last project I decided to take up some of my own adventures in code. To my surprise, I didn’t like the way codeIgniter loaded views anymore. I got used to the way opencart does it, by using breadcrumbs, a list of views you want to load, in an array, instead of
individual calls to the $this->load->view("viewname") method.

That is not the only problem I found. I also like to automatically load the language files of the views I’m using and pass them the data array I created on my controller and model calls. CodeIgniter, loads the view at the moment you call it, granted you can load it to a variable instead of outputting it but, if I later modify a variable used in those views, they keep the old value. This may be the expected behaviour for you, or may not.

I want to be able to tell my code which views I want to load, what data I want to pass it and when to start the output.
To solve this, I coded a very simple library that I called builder and while it’s still in development, since I’m still working on those projects, it saves me some time and makes my code more readable by doing some stuff for me in the background.

Not going to explain the code, just posting it to see if someone finds it useful.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Builder
{
    var $breadcrumbs = array();
    var $view = "";

    function __construct(){
        $this->CI =& get_instance();
    }

    /*
    * This function can be called with a string or an array as it's attribute
    * it will add the views that are not present in the $breadcrumbs array
    */

    function addCrumbs($crumbs)
    {
        if(!is_array($crumbs)) $crumbs = array($crumbs);
        foreach($crumbs as $crumb)
        {
            if(!in_array($crumb,$this->breadcrumbs))
            {
                $this->breadcrumbs[] = $crumb;
            }
        }
    }

    /*
    * It's a setter for the main view, granted we could just use
    * an equal sign, but I like this approach better since I could be picky
    * on what data can $view be set to
    */

    function setView($view)
    {
        $this->view = $view;
    }

    /*
    * Starts the output, explained in detail below
    */

    function output()
    {
        $data = empty($this->CI->data)? array() : $this->CI->data;
        //I get some data (if present) from the session variables and add it to
        //the $data array for easy access all over my views
        $data['user']['loggedIn'] = $this->CI->session->userdata('isLoggedIn');
        $data['user']['user_id']  = $this->CI->session->userdata('user_id');
        $data['title']  = '';
        $data['flash']  = $this->CI->session->flashdata('flash');
        $data['error']  = "<p>".$this->CI->session->flashdata('error')."</p>";
        $data['notice'] = $this->CI->session->flashdata('notice');

        if(!empty($this->breadcrumbs))
        {

        //For each breadcrumb (view) I load it's language and make it available by
        //merging the terms with the $data array once again.

            foreach($this->breadcrumbs as $view)   $this->CI->lang->load($view);
            $data = array_merge($data,$this->CI->lang->language);

        //For each breadcrumb (view) I load it's view as data and store it in the $data
        //array so it can be accessed from the main view, the str_replace solves the nested
        //controllers inside folder.

            foreach($this->breadcrumbs as $view)
                $data[str_replace("/","_",$view)] = $this->CI->load->view($view,$data,TRUE);
        }

        $this->CI->load->view($this->view,$data);
    }
}

?>

Granted some of the code will not be useful for you, since you don’t have the models I’m using, but the core functionality is an improved (for me) version of the view loading class native to codeIgniter. A sample usage, written just for the two followers I have could be something like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
class test extends Controller
{
    function __construct() // call the function test if php4
    {
        parent::Controller();
            $this->builder->addCrumbs(array('menu','userpanel','header','footer'));
            /* The previous line tells builder, I want to load all those views  
                in that particular order */

    }
   
        function index()
        {
                $this->builder->setView('test');
                $this->builder->output();
        }
}
?>

My view test would have all the breadcrumbs available as direct variables inside, so to echo my header I would just call echo $header; from inside my code. Since I loaded the language files before loading the views, my header will contain all the terms it needed and all parsed variables as well. It’s just convention to add language files for each view (in my case) so if you don’t have terms just have an empty file.

Note that the order of the views added to the breadcrumbs is important. All breadcrumbs have the previous ones available to use inside of them but not the other way around.

No posts at the moment. Check back again later!

About Juan

Hola! Me llamo Juan y soy un programador de 23 años de Madrid. Este es mi blog personal dedicado a la programación en general aunque con cierta predilección hacia PHP y Java.