I've been wondering 'bout this... I don't know much 'bout views, but can make a listing in code with the biggest ease. Takes me 'bout 3 minutes (5, max). I'm getting the impression that views are meant for listings, though. It just feels like so much hassle if I can just as well do it with no problem in code.

So, I've been wondering: When's it best to use views, and when's it best to go for coding?

Comments

SweeneyTodd’s picture

Views add formatting features that are compatible with the rest of Drupal. There are also come very nice features like adding sortable tables, AJAX paging for multi-page results, exposed filters and lots more that can be added with just a couple of mouse clicks. Also if someone else is going to maintain the site it means that they can do so without editing the code.

Custom code may be quicker to create for an experienced programmer and may be faster to execute, but there are also potential bugs and security issues that may be introduced.

Raf’s picture

So if I want to have, say, a list of news items, each with a styled title, a pic to the left, some text to the right, a read more link, comments that you can click to fold out (closed by default, to keep the whole list viewable), all of it in its own, styled box (consisting of some nested divs) and paginate it, I know it's no problem in code (already done each part of that separately in several occasions). How hard's it to do with views, though? Cause a simple list's easy and fast enough in views, but I suppose stuff like this can get pretty complicated and'd make grabbing to code seem like the best solution.

SweeneyTodd’s picture

I am not sure about the comments (only because I haven't dealt with them) but outputting the html for the rest via Views would only take a few minutes. I know there are options for the comments as well but I haven't needed to use Views for them before.

I get the feeling that no matter how hard I argue and try to convince you that using Views is quick and easy to maintain and modify that you will carry on writing the code instead.

Raf’s picture

Actually, nope. I'm at the point I got to do exactly what I described above. And yeah, I can do it with no effort in code. The fact is, though, that I haven't done so yet. That's because I want to see how easy it is to do with views. I constantly hear 'bout them on the forums, but never used them. Since so many people use them, they must be worth giving a try. That's why, no matter what (unless it's actually impossible), I'll do what I described above in views. There's simply no challenge to doing it in code. And that way, I'll learn how to use views, growing a bit as a Drupal developer (meaning someone who programs with Drupal, not someone who works on core. Just rectifying that, for good order!) But seeing how many people use views for things I've used code for, made me wonder when it's best to choose for one or for the other. That's actually what I wanted to ask. Same with the menu menu in admin part and using hook_menu, actually.

SweeneyTodd’s picture

One of the main reason for using Views is the plug-ins and integration with other modules. Views is one of the main interfaces for many of the image display programs and you also have Calendar and others. Once you get the hang of Views it is fairly quick to build basic queries and you open the door to all these modules as well.

Raf’s picture

Nice! Just gave this a try. Works nice'n fast (actually faster than through code if I got to check the database for each field) and I can theme it any way I want with a .tpl-file :D For simple listings, there's not much difference, but for more complicated stuff, views definitely seem faster, even if it's because I don't have to check several places, but just the views menu :D I even managed to add some Javascript to it (but that's through my .module-file, and the relevant DOM-elements in the .tpl-file). All in all, it only took me 1 hour - 1,5 hours, going from knowing barely anything 'bout views to knowing how to build a complex one, theme it and add the final touches. That's quite fast, going from having barely touched it twice to doing a full-fletched news listing with comments (got to fix the comments a bit, cause now they redirect to comments on another content type).

Of course, if someone wants to alter the view, they'll have to be able to alter theme files as well, but that's just a detail. I think that's not even that big a problem for people who don't use code, either.

SweeneyTodd’s picture

...do we have a convert from code to Views here?

Raf’s picture

Kinda. Still depends on what listing it is, but for complicated listings, views're less hassle (not necessarily easier, just less hassle). I'm now looking into how to use it for a listing on another site I'm working on, cause that's just that: a listing. Should significantly improve development time.

michelle’s picture

http://drupal.org/node/242311 may provide some insight.

Michelle