Welcome Guest, Not a member yet? Register   Sign In
Global View
#1

[eluser]jaswinder_rana[/eluser]
I have header and footer that I use in my views like

views/Sample.php
Code:
<?php echo $this->load->view('header');?>
BODY
<?php echo $this->load->view('footer');?>

I have sidebars both in header and footer. I want to include some items that I want to be displayed on ALL pages like "5 New listings" on left side and "Featured Listings" on right side.

I am not sure how to load those? I can create specific views like new.php and featured.php and then just load those.

My problem is, I am not sure how to load data for those views?

- Should I create a MY_Controller and then load that data in there?
- Should I create a library, put loader function in constructor and then autoload that library?

Thanks
#2

[eluser]Colin Williams[/eluser]
You could create a class (library) that handles the template parts (see my sig for one option) or extending the Controller class is always viable. For any app I've done that reaches a certain size and scope, extending the base controller for all requests is typically a must. No reason to avoid it.
#3

[eluser]jaswinder_rana[/eluser]
[quote author="Colin Williams" date="1231036162"]You could create a class (library) that handles the template parts (see my sig for one option) or extending the Controller class is always viable. For any app I've done that reaches a certain size and scope, extending the base controller for all requests is typically a must. No reason to avoid it.[/quote]
Thanks.

Even if I use your approach, I'd still have to load that data somewhere. And calling that function in every controller doesn't seem sensible to specially if I want to change that content later.

So, from library and controller option, which one would you go with? Which is more manageable later on?
#4

[eluser]Colin Williams[/eluser]
The phrases "global" or "in each controller" in CI-speak is nearly always synonymous with "MY_Controller" Smile You can also copy system/libraries/Controller.php to application/libraries/Controller.php and just add your stuff onto it (good place for validation callbacks too)
#5

[eluser]jaswinder_rana[/eluser]
That was quick.
Thanks Colin. MY_Controller it is.
#6

[eluser]jaswinder_rana[/eluser]
I did add it but it din't work.

I then searched in the forum and found that I need to change my controllers and change "extends Conntroller" to "extends MY_Controller" (I am calling it MY_Controller).

Is there a way around this? I am hoping not to do this as I have so many files. And if later on I want to change it then it'd be pain.

Lets say that I do change my views and load the data, but how can I pass it globally so that any view can access it? I am sending data to my views via $data array and then pass to it when I load that view ($this->load->view('view',$data)Wink

Thanks
#7

[eluser]jaswinder_rana[/eluser]
I found $this->load->vars() function and it's working (in MY_Controller contructor).

Is there another better way to do it?




Theme © iAndrew 2016 - Forum software by © MyBB