Code / Appnel Solutions 

Posted
8 October 2007 @ 2pm

Easy Count Labeling in MT4

If you've done any amount of blog reading you've probably seen labels or links that read "1 Comments." Perhaps if the designer was a bit more grammatically aware they placed the S in parenthesis, i.e. "1 Comment(s)." This is so common, not because the template designer is illiterate, but because they just did the quick and obvious thing:

<$mt:entrycommentcount$> Comment(s)

This seems a bit lazy to me since you can do better with a bit of extra effort. Here is a MT4 solution that will output the grammatically correct label:

<mt:setvarblock name="comment_count"$><$mt:entrycommentcount$></mt:setvarblock>
<$mt:entrycommentcount$> <mt:if name="connent_count" eq="1">Comment
  <mt:else>Comments</mt:else></mt:if>

This would output "1 Comment" and "2 Comments", but this is a bit convoluted and a slightly annoying to have to remember each time you need a count label. There is a better way though.

The Gizmos for MT collection provides a number of tags that make this easier to do. Each object (content or record in the database) now has a singular and plural label that the mt:countlabel makes use of. With that bit of data, the mt:countlabel tag can wrap up the logic of the above example (and then some) into one neat and tidy variable tag.

<$mt:entrycommentcount$> <$mt:countlabel class_type="comment"$>

By taking a class_type argument this tag is not only limited to comments, but TrackBack pings or even entries in an archive. Working from the class type this tag could also support objects added by other plugins given the proper context.

We can do even one better though. If we know what we are labeling then we can get the count also. The mt:countheading combines the above example into one tag. Now all you need:

<$mt:countheading class_type="comment"$>

The output is the same, but now with only one tag. Easier on the eyes and easier to remember.

One thing that mt:countheading does that may not be ideal is output labels like "0 Comments." Many may want to output an entire different message then display a big zero. We can address this with a mt:countiszero conditional tag in the Gizmos collection:

 <mt:countiszero class_type="comment">No Comments Yet
   <mt:else><$mt:countheading class_type="comment"$></mt:else>
   </mt:countiszero>

Now when the comment count of an entry or page is 0 instead of "0 Comments" the phrase "No Comments Yet" is output. I thought about making this a feature of mt:countheading, but thought the zero count label would often vary to often that I chose not to implement it.

For more details on these "gizmos" and more see the Gizmos for MT documentation in the knowledge base.


← Before After →