[development] Styling comment blocks according to vote results

Larry Garfield larry at garfieldtech.com
Tue Jan 16 14:56:46 UTC 2007

That's not quite how I'd do it, but it's close.

You probably don't want that sort of logic in comment.tpl.php.  Instead, I'd 
have a theme function that themes, well, a class. :-)  Pass it your raw data 
or some partially processed version thereof, and it returns a simple class 
string that you assign to the comment.  A simple implementation could be:

// Number of votes for each rating
$scores = array(1=>5, 2=>3, 4=> 7, 6=> 2);

theme_mysummary($scores) {

  // Get an average of all scores, or a median, or whatever
  if ($avg < 2) return 'sucks';
  if ($avg < 4) return 'ok';
  if ($avg < 7) return 'good';
  return 'rocks';


Then you just assign the string returned from that theme function as a class 
of the comment with a prefix, say "mysummary-{$summaryclass}".

And the rest is up to CSS.  Since it's a theme function you can easily 
override it in template.tpl.php with whatever logic you want for a given 

At least that's my early morning thought on the matter before I've had my 
morning up of tea. :-)

On Tuesday 16 January 2007 6:26 am, Ben Campbell wrote:
> Hi there - I'm working on a system to change the appearance of comments
> based on how people rate them. So highly rated comments would appear
> bigger and brighter than badly-rated comments.
> The voting API abstracts away the rating mechanism nicely (so in theory
> you could pick any voting module to drive the ratings system).
> But I'm not 100% sure of the best way to apply different looks to
> differently rated comments.
> My current thinking is that it'll have to be pretty tied to a theme -
> I'd pass some sort of rating metric into comment.tpl.php as an extra
> parameter and the template applies an appropriate style (probably one of
> a range of styles defined in a stylesheet).
> Does this sound like a sane way to do it?
> Does anyone have any suggestions of approaches I could take to
> generalise it a little? It'd be nice to decouple it from the theming as
> much as possible so others can take advantage of it.
> Thanks!
> Ben.

Larry Garfield			AIM: LOLG42
larry at garfieldtech.com		ICQ: 6817012

"If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it."  -- Thomas 

More information about the development mailing list