Welcome Guest, Not a member yet? Register   Sign In
How do I include this data in every page of my site?
#1

[eluser]KeyStroke[/eluser]
Hi,

I'm coding a site where every page has a side bar that shows the user's information (profile picture, number of unread messages ....etc).

The side bar of course is included in every "View" in the project, but I don't want to load up the users' data in every single Controller's functions to send them to the view.

So, is there anyway to query and include this information directly from the View?


Appreciate your help Smile
#2

[eluser]Jelmer[/eluser]
Just put your sidebar in an extra view file and load it from within every view you want it in.
#3

[eluser]KeyStroke[/eluser]
Thanks Jelmer, but I'm afraid that's the part I know.

The part I don't know is how to use a controller's (or a model's?) functions from within the view. Smile
#4

[eluser]Jelmer[/eluser]
Sorry, read your post a bit too quick.

This is how to use a model:
Code:
$ci =& get_instance();
$ci->load->model('Your_model');
$ci->Your_model->your_function();

Works the same for a library, a controler's functions aren't available as far as I know.
#5

[eluser]Rick Jolly[/eluser]
Modular Extensions HMVC.

HMVC allows you to load a view and its data from within a view. It's clean and modular since the controller doesn't have to load the sidebar data. This reduces dependencies and decreases code duplication. Plug and play.
#6

[eluser]KeyStroke[/eluser]
Jelmer:
Thanks. But this will force me to use a Model technically as a Controller, right? it works, but it doesn't feel right to be honest.

Rick Jolly:
I checked it out, while it seems very useful, both HMVC and Matchbook have very complex documentation that lack examples. Got alternative or better resources to learn these?
#7

[eluser]Jelmer[/eluser]
Keystroke,

I must admit I don't enjoy this method that much myself. But I wanted ultimate flexibility in my views, I have different templates that need different menu generating functions which are in the views themselves (so it's more like using the view as a controler, the model stays a model). This was the only way I could get it to work.

I know about HMVC but I've never taken the time to learn about it properly, and this solves the problem for me.
#8

[eluser]Randy Casburn[/eluser]
Here's an option:

Welcome controller includes and extends Core...
Code:
include_once($system_folder.'/'.$application_folder.'/controllers/core.php');
    
class Welcome extends Core {

    function Welcome()
    {
        parent::__construct();    
    }
    
    function index()
    {
        $this->load->view($this->settings['unsecure_home'], $this->settings);
        parent::__destruct();
    }
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */

Core Controller extends Controller...
Code:
class Core extends Controller {
    
    var $settings;
    const header = 'common/header';
    const footer = 'common/footer';
    
    function __construct()
    {
        // Fetch Required Libraries
        parent::Controller();
        $this->__init();
        // load common views that go before your page loaded view here
        $this->load->view(self::header, $this->settings) ;
    }

    function __destruct()
    {
        // load views  that go after your page loaded view here
        $this->load->view(self::footer, $this->settings);
    }


    function __init()
    {
        $this->settings['unsecure_home'] = 'welcome_message';

        // do page setup stuff here
        // call common model stuff here
    }
    
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */

You can __init() to load your common Models or data or whatever your choose. Use the constructor or whatever to load your views as stated and use the destructor to load 'after' views.

Might work for you for simple layouts.

Hope this is helpful,

Randy
#9

[eluser]Rick Jolly[/eluser]
Check out this post for a basic idea:http://ellislab.com/forums/viewthread/58405/#287660

However, this is the part of modular extensions that you need to know for loading views within views:
Code:
<?php echo modules::run('module', $data, 'method') ?>




Theme © iAndrew 2016 - Forum software by © MyBB