CodeIgniter Forums
Better way to handle data passed to views? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Better way to handle data passed to views? (/showthread.php?tid=7417)



Better way to handle data passed to views? - El Forum - 04-08-2008

[eluser]bcorcoran[/eluser]
OK, I've been wondering this for a while but haven't asked. Is there a better way to handle sending data to the view than the following:

Code:
<?php

class Site extends Controller {
    
    function __construct()
    {
        parent::Controller();
    }
    
    function index()
    {
        // Load Config
        $data['config'] = $this->config->item('site_config');
        $data['page'] = "Home";
        
        // Load View
        $this->load->view('site/home', $data);
    }
    
    function about()
    {
        // Load Config
        $data['config'] = $this->config->item('site_config');
        $data['page'] = "More Info";
        
        // Load View
        $this->load->view('site/info', $data);
    }
    
}

?>

My question is that I have to duplicate loading the config item for every function, and it's always going to be the same for each one of the functions in this particular controller (it's for static pages -- eschewing the db for now).

Is there a better way where I don't have to replicate this every time? I'm already loading my config file with autoload, and then I'm loading the site_config array from it.

Another note: I have to include this config array in *every* controller function which contains a view... I don't really know of a better way to do it.

Thanks for reading!


Better way to handle data passed to views? - El Forum - 04-08-2008

[eluser]xwero[/eluser]
in your view you can use
Code:
<?php echo $this->config->item('site_config'); ?>
or you can do something like
Code:
class Site extends Controller {
    
    protected $data;    

    function __construct()
    {
        parent::Controller();
        $this->data['config'] = $this->config->item('site_config');
    }
    
    function index()
    {
        $this->data['page'] = "Home";
        
        // Load View
        $this->load->view('site/home', $data);
    }
    
    function about()
    {
        $this->data['page'] = "More Info";
        
        // Load View
        $this->load->view('site/info', $data);
    }
    
}
If you want to use this in all your controllers you can extend the base controller and put the variable there.


Better way to handle data passed to views? - El Forum - 04-08-2008

[eluser]bcorcoran[/eluser]
Ah, very nice, thanks. I'm still pretty intermediate at PHP so I am not familiar with certain concepts.

Also, you missed one thing in your modified example:

Code:
$this->load->view('site/home', $data);
needs to be
Code:
$this->load->view('site/home', $this->data);

Otherwise, it works great. Thanks!


Better way to handle data passed to views? - El Forum - 04-08-2008

[eluser]xwero[/eluser]
you're on your way to become an advanced phper Wink


Better way to handle data passed to views? - El Forum - 04-08-2008

[eluser]wolfman2000[/eluser]
Personally, I prefer having a separate Library handle the views. Here's my Paste.

Feel free to use/modify this. I doubt most of you guys will be using "Pump Pro Edits" as the main title, or using the profiler, etc. It's what I'm currently using on my development server.


Better way to handle data passed to views? - El Forum - 04-08-2008

[eluser]bcorcoran[/eluser]
[quote author="wolfman2000" date="1207684690"]Personally, I prefer having a separate Library handle the views. Here's my Paste.

Feel free to use/modify this. I doubt most of you guys will be using "Pump Pro Edits" as the main title, or using the profiler, etc. It's what I'm currently using on my development server.[/quote]

Would you care to show an example of how you use this? (i.e. sample controller/view snippets)


Better way to handle data passed to views? - El Forum - 04-08-2008

[eluser]wolfman2000[/eluser]
I'm not at my development computer right now, but I am using what may be a non traditional approach.

Each unique page has their own controller: one controller for news updates, one for credits, one for specific content, etc. Inside the index() function (or one of the other sub functions), I call my library with the page I want to load. The library then takes care of the rest. If I want to supply additional/extra data, I do so in the controller before passing $data.

I can explain more later upon request.