Need someone to lead product management at your software company? I build high-craft software and the teams that build it. I'm looking for my next opportunity. Check out my resume and get in touch.

This is the blog of Adam Kalsey. Unusual depth and complexity. Rich, full body with a hint of nutty earthiness.

Content Management

Smart Comments for Movable Type

Freshness Warning
This blog post is over 22 years old. It's possible that the information you read below isn't current and the links no longer work.

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>

Recently Written

Think Systems, not Symptoms
Dec 15: Piecemeal process creation frustrates teams and slows work. Stop patching problems and start solving systems. Adopting a systems thinking approach helps you design processes that are efficient, aligned with goals, and truly add value.
Your Policies Aren’t Your Culture
Dec 13: Policies guide behavior, but culture is the lived norms and values of your team. Policies reflect culture -- they don’t define it. Netflix’s parental leave shift didn’t change its culture of freedom and responsibility. It clarified how to live it.
Lighten Your Process Burden
Dec 7: Everyone hates oppressive processes, but somehow we keep managing to create them.
Product Add-Ons Are An Expansion Myth
Dec 1: Add-ons can enhance your product’s appeal but won’t drive significant market growth. To expand your customer base, focus on developing standalone products.
Protecting your Product Soul when the Same Product meets New People.
Nov 23: Expand into new markets while preserving your product’s core value. Discover how to adapt and grow without losing your product’s soul.
Building the Next Big Thing: A Framework for Your Second Product
Nov 19: You need a first product sooner than you think. Here's a framework for helping you identify a winner.
A Framework for Scaling product teams
Oct 9: The people, processes, and systems that make up a product organization change radically as you go through the stages of a company. This framework will guide that scaling.
My Networked Webcam Setup
Sep 25: A writeup of my network-powered conference call camera setup.

Older...

What I'm Reading