CodeIgniter Forums

Full Version: Advice on views for comments system of very dynamic website
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
On my site I will have posts called creations. There will be about 10 to a page. The user will have the option of leaving a quick comment, or leaving a critique. If they want to leave a critique then a critique form pops up which contains suggested topics that the owner of the creation wanted feedback on. Users will be able to leave comments on the critiques of others too. There will probably be about 10 creations to a page with a next button for more (not a fan of infinite scroll). I will use ajax to fetch the creations.

I'm just wondering. If I were to do most of my rendering server side. What would be a be a good way of organizing the views for updating this creation posts and their comments and critiques (which also could have comments)

Do I have one view that deals with everything to do with the creations, critiques, and comments. If I did this, what if the user has clicked a 'see more' link to reveal more comments on a critique. I would then have to tell this view to not redraw the whole creation post and critiques, but just the comments. So this makes me think maybe I should have a separate view for just comments this. I can't imagine I nice way to organize this. Or do I redraw the whole thing, and just delete the old one on the client side and add the new one?

Any advice on how to organize the views and passing the creations, comments and critiques? Do I need lots of separate mini views?

I'm tempted to just do the whole system in javascript. I think it could be easier as I could have creation objects, that have arrays of comments and critiques, and each could have it's own listeners. Then it would be easy to just get the information from the server as json. This seems less complicated to me, but people who talk about client side rendering talk about using a js framework. I'm really not in the mood try and lean another framework and follow all the zillions of what not to do rules.
I would have a view with the layout for a single comment, one for a single critique etc.

Then just load that view, passing across the data for the specific item, using a loop. You could do this via an ajax request and then append the HTML returned, if it was for a "see more".

Otherwise, just have a foreach loop for the initial comments you want loaded, and load the comment view over and over, passing the item across to the view file.
Thanks for replying. That sounds great.

So is loading a view in codeigniter not slow performance wise?I suppose because it's called 'loading' it made me think it was something I shouldn't do over and over in a loop.
(07-16-2015, 06:32 AM)DreamOfSleeping Wrote: [ -> ]So is loading a view in codeigniter not slow performance wise?I suppose because it's called 'loading' it made me think it was something I shouldn't do over and over in a loop.

PHP is reasonably good at optimizing something like this. However, you should evaluate the performance based on real-world examples for your application. It is quite possible that loading the view in a loop will mean repeatedly going to the file system to retrieve the view, then evaluating the PHP in that view every time you go through the loop. That's probably the worst-case scenario you should expect.

You don't want to create some complicated way of avoiding the worst-case scenario until you've evaluated the performance of that scenario to determine whether it's worth the extra effort. I don't know how many times I've run profiler on a slow page expecting to start working on one part of the page to speed things up, only to find that the code on the page which takes up the most execution time is something unrelated.
I did fine this on stack overflow http://stackoverflow.com/questions/14690...op-in-view

It seems that it is slower to load the view over and over. It also seems really messy to me.

I thought maybe I could have a php class with functions that I could just send data too and they could either render or return as a string and act as a mini view That means I could have a few different views that call these functions, and not have to have duplicate code or load a view. I would just have to include the class once.

I think I've spent too much time worrying about what's the right thing to do, and I should just do it. I'm starting to psych myself out.

Thank you for the advice.
See, and for me, with the numbers for 1004 results in that post, choosing between 1.1x and 1.9x seconds is a non-starter. I'd want to know where the code spends 1 second doing something before I started looking at the different methods of loading the data into a view (or views). That said, given the views used in the scenarios in that post, my view would usually look more like scenario 2 (in fact, I would build the whole table in the view, not just the body of the table).

I think the most important thing is to start by building the page(s) using whatever structure makes sense to you at the time. Once you have things up and running you can evaluate the performance and determine where to spend your time trying to improve the page(s).

Finally, use a debugger to profile the application. There's only so much information available within CodeIgniter. If I have a page that's taking too long to load, I want to know how long the application is spending in each class/method/function before I spend time rewriting anything. In one case I found that the single change I could make that would speed up the page the most was to go through and replace every instance of a helper function with a direct call to the library which that helper function references (it was similar to calling $this->config->site_url($uri); instead of site_url($uri)), but I don't do it throughout my code because, most of the time, the get_instance() call in the helper is insignificant.
Thanks again.

"I think the most important thing is to start by building the page(s) using whatever structure makes sense to you at the time. Once you have things up and running you can evaluate the performance and determine where to spend your time trying to improve the page(s)."

I totally agree, but I'm so so new to building websites, and codeigniter. I wanted to make sure I'm not making some terrible terrible mistake. Mainly because i'm so slow, it's a big commitment to go in any direction.
Additionally, if loading of views becomes an issue for many results, there are a couple of ways that you can modify things to negate that performance hit and only load the comment view once.

The first would be to have the view simply contain a loop, and you pass all results to it at once, then the view just loops over all of the comments.

The second way would be to have the view contain placeholders, much like you would find in a Mustache or Handlebars template, where the variables are wrapped in curly braces. Then, load that view up into a variable. Next loop over all of your comments, doing str_replace or strtr on the template to create the final output.

Even past all of that, though, you can start adding in caching if needed, or a Varnish cache on the server, etc.

As Mat said "start by building the page(s) using whatever structure makes sense to you at the time" and only worry about it if it becomes too much of an issue. There are quite a few ways that it can be tackled, so no worries.