• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how do you organise your code so you can re-use things?

#1
[eluser]compoundeye[/eluser]
Hello,

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

The database has a small number of tables:
Quote:
song
composer
rating
performance
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

mathew

#2
[eluser]Sumon[/eluser]
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.

#3
[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.

#4
[eluser]compoundeye[/eluser]
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.

thanks

mathew

#5
[eluser]drewbee[/eluser]
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.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.