Welcome Guest, Not a member yet? Register   Sign In
how do you organise your code so you can re-use things?


I'm working on a site for a music/art/engineering project.

The database has a small number of tables:
Each table has a model object

Information from these different tables appears in different places throughout the site - for example:

A song list widget might appear on lots of different pages throughout the site

I'm assuming that i don't want to put all the query and formatting code in the controller and a single full page view, as I'll only end up copying and pasting it into other controllers and views

In the past I've used libraries that used lots of little mini views that were put together to make a page.
But maybe there's a better way?
What do you do when you know you'll want to use a piece of data and formatting in lots of different parts of a site?

Thanks for your help


Lets say we have two controller named song and composer.Moreover, both have different properties and functionalities. so that we have a number of different methods for song and composer. In addition, song and composer have different model as well(to handle database operations).

Well at this point lets think, we need a list of songs by singer SUMON in performance controller. so what we should do? if we use song model then we have only data from database without having good amount of logics. But if we call song::SongsByArtist('SUMON') function then it cotain data with some logic applied.

Make sure you have not call any view file in SongsByArtist() function. This function only read data from database and apply some logic and return some data.

Finally pass these values into your view. Same approach can be applied by you for view file. Break down the main view file into sub views and then pass necessary values into sub view.

I am also looking for a better reply then mine.

[eluser]Colin Williams[/eluser]
Quote:A song list widget might appear on lots of different pages throughout the site

The operative phrase there is "appear on." Most people don't have a hard time abstracting data loading, abstracting validation, abstracting common subroutines, but yet so many choke when it comes to abstracting how content makes its way to the client. You have to think beyond "ok. I'm at my controller. Load me some data then load me a view or two." You are essentially cutting off all other actors in your application.

You might find it useful to employ a library like Modular Extensions HMVC (look it up in the wiki), but even then you are still just abstracting background processes and not necessarily what rises to the top.

I know none of this is clear or concrete (and CI doesn't come with an answer in itself), but you need to do your best to extrapolate.

thank you both for your help.

being able to abstract the presentation is exactly the missing piece I couldn't work out.

I had sort of solved the problem by using multiple views that were composited together, the it was so cumbersome and complex it didn't seem worth the effort for the greater theoretical 'flexibility'

modules though look like they will allows me to do exactly what i have been hoping to do.



What about creating a mini-view (module) so to speak.

I would personally create a MV approach to this one. The model gets the data, the view displays it. This mini-module MV is then called on the displaying controller page via our standard methods. Nothing wrong with having multiple models/view calls within a single controller.

The little template library that I wrote and use follows this. Create these views so that they are not a full html page, but a section of it. Then load it into the parent template.

Theme © iAndrew 2016 - Forum software by © MyBB