Welcome Guest, Not a member yet? Register   Sign In
Modular Views
#1

[eluser]Unknown[/eluser]
Hello I have been using CodeIgniter for over a year now and have recently starting using a second php mvc framework for a different (already in progress) project. One very useful feature that I found in this second framework is the idea of "modular views". I am wondering if I have been missing something in CodeIgniter and if this can actually indeed be accomplished with CI. Let me explain first...

So in CI I have a view called mycontacts.php. This view is a little box that goes in a sidebar and displays my 5 newest contacts. So in my Welcome::index() function for example, I do the php processing to get my 5 newest contacts and store it in a $data array. This variable, along with some other variables are passed into my index.php view and then the $data array of my 5 newest contacts is passed when loading the mycontacts.php view. My 5 newest contacts are properly displayed. Now lets say on my profile page...i want to display this as well. I can resuse the view obviously, but now in the Profile::index() function, I need to do that same php processing to get get my 5 newest contacts again...and pass it along to the mycontacts.php when loading it from the profile index view. If I want to load this mycontacts.php view 50 times....you see where this is going.

In framework #2 loading a view is directly correlated to one controller::function(). so instead of having to do php processing every time i want to display my 5 newest contacts, i just have a function in lets say my user controller called "mycontacts()". anytime i load the mycontacts.php view, the php processing occurs within that User::mycontacs() function and is then available within the mycontacts.php view. Note that the User controller is specified when loading the mycontacts.php view in framework #2, so mycontacts() function is only in User controller. You can see how much more modular this is, cleaner it is, reusable, and how much less repetitive code you need to write.

Am I missing something in CI? Can this be done? This seams like a must have.

Thanks!
Kevin
#2

[eluser]obiron2[/eluser]
What I have started doing is this:-

I have a master view which renders a 2 column layout. in the view each column does a $this->load->view($main_view) or $this->load->view($side_view)

I set the relevant information for the view in the main controller and assign it to $data['whatever_i_need']

and set the renders to be $data['main_view'] = 'this_is_my_main_view', $data['side_view'] = 'links_view'

any data passed to the master_view can be accessed by the sub_views. Anywhere you need to do your contacts you could put into the view

if isset($contacts){$this->load->view('contacts_view');}

This way if you have not set the $data array element ['contacts'] the thing won't fall over.

Hope this helps. I can post the exact code extracts if you want.

Obiron
#3

[eluser]Unknown[/eluser]
hmm ok thanks. this is def a good alternative to my goal, but i am still interested in finding some other ways to do what im trying to accomplish. thanks again.




Theme © iAndrew 2016 - Forum software by © MyBB