Smart Comments for Movable Type

Freshness Warning
This article is over 16 years old. It's possible that the information you read below isn't current.

If you take a look at the front page of this Weblog, you’ll notice that, unlike most Movable Type Weblogs, the comment count doesn’t say "0 comments, 1 comments, 2 comments." Instead, it is grammatically correct and, if there is only one comment, the singular is used.

Movable Type provides an option for inserting a count of an entry’s comments, but only the number is inserted, leaving the site owner to fill in the word "comments" or "replies." This creates a problem in that if a template developer wishes to say "1 reply" instead of the incorrect "1 replies," they are unable to do so.

So how is this done?

There are various workarounds available in the Movable Type support forums for grammatically correct comment counts using JavaScript, Extended SSIs, or PHP, but these methods require server-side processing or the presence of JavaScript in the user’s browser.

Enter regular expressions. Regular expressions (regex) allow powerful text matching and replacement and are available in most scripting languages, including Perl and JavaScript. And now Brad Choate has written a regex plugin that allows Movable Type templates to use them as well.

To use the template code that comes next, you will need to have both the regex plugin and Brad Choate’s global tag plugin installed in your plugins directory.

With those plugins installed, you can then use the MTAddRegex tag inside your entry templates.

Let’s examine a typical template’s code for displaying the comment count.

<a href="<MTEntryLink>"><$MTEntryCommentCount$> replies</a>

We have created a link using a href that ends with >. Immediately following, we have a number representing how many comments there are, followed by the word "replies" and the close of the anchor tag(</a>)

Here’s what the output of this would look like for 0, 1, and 2 comments:

0 replies
1 replies
2 replies

If there are no comments, we’d like to simply display "Reply" and if there is one comment for an entry we’d like to display "1 reply." Entries with two or more replies will continue to look the same.

First, you will need to add two regex replace commands to the top of your template.

<MTAddRegex name="reply0">s|>0 replies|>Reply|g</MTAddRegex>
<MTAddRegex name="reply1">s|>1 replies|>1 reply|g</MTAddRegex>

The first line searches for the end of the link code, followed by "0 replies" and replaces it with the text >Reply. The second line searches for >1 replies and replaces it with >1 reply. The > character is included at the beginning of each search and replace so to make sure we replace the right thing. If one of your entries happens to contain the text "0 replies," you wouldn’t want to accidentally replace it with "Reply." We’ve also given each regex tag a name so that we can refer to them later.

Now that we have put the regex commands into the template, we need to tell your template to use them.

Change your MTEntries tag to include an attribute called regex that contains the names of the regex tags we created: <MTEntries lastn="10" regex="reply0 reply1">

This will tell Movable Type to run the search and replace on anything that appears inside the MTEntries container, and to run the regex called reply0 first and the one called reply1 second.

Now on to the comments tag. You need to make sure that there are no spaces between the end of <a href="..."> and <$MTEntryCommentCount$>. If you do have extra spaces in there, the regex won’t be able to find it. The entire comment count section should look like: <a href="<MTEntryLink>"><$MTEntryCommentCount$> replies</a>.

Now you’re done. Take a look at what a Movable Type template might look like with these changes.

<MTAddRegex name="reply0">s|>0 replies|>Reply|g</MTAddRegex> 
<MTAddRegex name="reply1">s|>1 replies|>1 reply|g</MTAddRegex> 
...
<MTEntries lastn="10" regex="reply0 reply1"> 
	<h1><$MTEntryTitle$></h1>
	<p><$MTEntryDate format="%B %e, %Y"$> - 
	<MTEntryIfAllowComments> 
		<a href="<MTEntryLink>"><$MTEntryCommentCount$> replies</a>
	</MTEntryIfAllowComments></p>
	<p><MTEntryBody></p>
</MTEntries> 

You can change the text that you use to describe your comments to anything you would like. For instance, if an entry has no comments and you want to display, "Why don’t you comment," you only need to change the line <MTAddRegex name="reply0">s|>0 replies|>Reply|g</MTAddRegex> to read <MTAddRegex name="reply0">s|>0 replies|>Why don’t you comment|g</MTAddRegex>

Brad Choate
July 10, 2002 9:33 AM

Another technique for this is to apply a regex to just the comment count tag. Create you regex like this (this should all be on a single line even if it appears to be on multiple lines as displayed here): <MTAddRegex name="reply_count">s/(\d+)/$1==0?'Reply':$1==1?'1 reply':$1.' replies'/e</MTAddRegex> Now for your comment count tag, write this: <a href="<MTEntryLink>"><$MTEntryCommentCount regex="reply_count"$></a> This should produce the same effect. Note that the ' replies' text doesn't have to be in the template since the regex adds it appropriately depending on the count.

Adam Kalsey
July 10, 2002 9:46 AM

This is actually the code that's used on the index template of this site. It's quite a bit faster during the rebuild than the code listed in the article, but is a little more difficult to follow and customize if you don't understand regular expressions. You'll also need the latest version of Brad's regex and global attribute plugins to get this code to work.

Richard Ames
February 8, 2003 8:03 PM

Adam- I want to install smart comments on my MT site. I see I need Brad's "global tag plugin" (I've already uploaded regex). However Brad seems to have 3 different global tag plugins. Which one do I need for smart comments (his site doesn't say), or do I need all three? Thanks. Richard

Adam Kalsey
February 8, 2003 8:08 PM

You only need the global attribute plugin in order to get the regex plugin working. So if you've successfully used the regex plugin, then you're set. If you are using MT 2.5 or later and a recent version of the regex plugin, then you won't need the global attributes plugin at all.

Richard Ames
February 8, 2003 8:15 PM

Yes, I'm using 2.51. Great. Guess I was looking at some outdated documentation. Thanks for your help. I do appreciate it.

Tom Harpel
November 2, 2003 1:34 PM

This worked just great for me. What a nerdy thing to do with a Sunday afternoon. Thanks for posting the info.

Lonnon Foster
December 16, 2003 5:16 PM

Thanks for the excellent tip. I'd worked around this problem before by creative use of English; basically, I omitted the word "comments" after the numeral, like this: 0 so far. 1 so far. 2 so far. Now I can have something sensible: No comments yet. 1 comment so far. 2 comments so far.

J
September 12, 2004 1:28 PM

Thanks for this tip. I tried it. Works fine. 1 problem I encountered, though, is that it also replaces the numbers posted in the comment body.

This discussion has been closed.

Follow me on Twitter

Best Of

  • The mouse and me Not only is the mouse very destructive, but it's evaded all attempts to capture or kill it so far.
  • Lock-in is bad T-Mobile thinks they'll get new Hotspot customers with exclusive content and locked-in devices.
  • Newly Digital Newly Digital is an experimental writing project. I've asked 11 people to write about their early experiences with computing technology and post their essays on their weblogs. So go read, enjoy, and then contribute. This collection is open to you. Write up your own story, and then let the world know about it.
  • Pitching Bloggers Forget what you learned in your PR classes. Start acting like a human instead of a marketer, and the humans behind the blogs will respond.
  • California State Fair The California State Fair lets you buy tickets in advance from their Web site. That's good. But the site is a horror house of usability problems.
  • More of the best »

Recently Read

Get More

Subscribe | Archives

16

Recently

Stretching your team (Jun 11)
Stretching your team is one of the best ways to improve your output, your team's happiness, and your velocity. But they'll need coaching.
Physical camera shutter for Cisco Spark Board (Jul 6)
A 3d printable design for a camera shutter for a Cisco Spark Board
My Travel Coffee Setup (Jan 20)
What my travel coffee brewing setup looks like, and how you can build your own for under $100.
Turkey Legs (May 30)
Product naming gone awry.
Speaking for Geeks: Your Slides (Dec 17)
Tips and tricks for creating great slides.
Speaking for Geeks: Writing Your Talk (Dec 14)
Don’t wait until the night before the talk to write it. Crazy, I know.
Speaking for Geeks: Tell a Story (Dec 13)
Telling a story keeps your presentation focused, keeps your audience interested, and makes it easier for you to remember your talk.
Speaking for Geeks: Where to speak (Dec 11)
You've got a great idea for a talk. How do you find conferences to submit it to?

Subscribe to this site's feed.

Elsewhere

Tropo
Voice and communications platforms, including Tropo and Phono. Work.
SacStarts
The Sacramento technology startup community.
Pinewood Freak
Pinewood Derby tips and tricks

Contact

Adam Kalsey

Mobile: 916.600.2497

Email: adam AT kalsey.com

AIM or Skype: akalsey

Resume

PGP Key

©1999-2018 Adam Kalsey.
Content management by Movable Type.