Movable Type plugin: MTSwitch

This is my very first attempt at writing a plugin for Movable Type. MTSwitch is a plugin that provides Movable Type with basic Switch/Case-functionality.

Updated: MTSwitch no longer inserts extra blank lines.

If you're a programmer, you should already know what Switch/Case does, but just in case you're not: A switch-case statement provides a convenient way to compare one value against a list of other values, and perform whatever is within the matched block.

Download

Download the plugin in zip format.

Current version

(2004-02-26) - 0.14: This is a fix for a bug that caused 500 Internal Server Errors under certain enviroments (cgiwrap, mainly).

Installation

Copy the plugin to the Movable Type plugins directory

New Tags

MTSwitch provides three new template tags:

  • <MTSwitch>: Container tag to set up the switch/case context.
  • <MTSwCase>: Container tag used to output content if the tag's value attribute matches the value attribute of <MTSwitch>.
  • <MTSwDefault>: Container tag used to output content if none of the preceding case statements have matched.

The <MTSwCase> and <MTSwDefault> must be used inside of the <MTSwitch> tag.

The 'value' attribute

Applies to: <MTSwitch>, <MTSwCase>

The value attribute is available in the <MTSwitch> and <MTSwCase> tags, and provides the value to compare.

The value attribute may contain any Movable Type tag that returns some content, enclosed in square brackets, e.g.: <MTSwitch value="[MTEntryCommentCount]"> or <MTSwCase value="[MTEntryTrackbackCount]">. If you use a MT tag that accepts attributes inside the 'value' attribute, replace any double quotes with single quotes.

Usage example

If you're still puzzled, here is a quick example - which is very similar to the one I use to create the grammatically correct 'Comment' count in my postings:


<MTSwitch value="[MTEntryCommentCount]">
  <MTSwCase value="0">
    <a href="<$MTEntryPermalink$>#comments">
    No comments yet</a>
  </MTSwCase>
  <MTSwCase value="1">
    <a href="<$MTEntryPermalink$>#comments">
    1 Comment</a>
  </MTSwCase>
  <MTSwDefault>
    <a href="<$MTEntryPermalink$>#comments">
    <$MTEntryCommentCount$> comments</a>
  </MTSwDefault>
</MTSwitch>

If the comment count was 0, the plugin would output No comments yet, if the comment count was 1, the output would be 1 comment. For all other values, the output would be n comments, where n is the number of comments.

Known bugs and limitations

  • Switch statements may not be nested.
  • Switch comparisons are case sensitive. This is intentional.

Questions

I prefer that any questions be directed to the comment section of this entry, but if you wish to contact me in private, check my contact page for contact information.

Disclaimer

Again, this is my very first plugin for Movable Type, and it's also my first attempt at getting my hands dirty with Perl. I am using it myself, and has seen no problems other than the ones already mentioned. Should you choose to use the plugin, you do so at your own risk.

So, if this plugin causes your cat to be run over, your dog to catch fire, your wife or husband to leave you, or your MT installation to explode, that's entirely your problem. :-)

Comments

Comment from Donnie on 2003-05-31 16:40

Great plugin…I’ll put it to good use, thank you

Comment from Tommy on 2003-06-02 13:41

Kjekk plug-in. Er nå installert på mine sider.

Comment from Matt Powell on 2003-08-21 05:44

Thanks for a great plugin. One suggestion, though: in the _MTSwCase subroutine, instead of $test eq $switch_val, why not have $switch_val =~ $test? That way, you can have multiple branches sharing code.

Comment from Arve on 2003-08-22 10:44

Matt, thanks for the suggestion. I’ll see what I’ll end up doing, as I’ve had conversations with someone in the process of writing something that will replace MTSwitch and a bunch of other similar plugins.

Comment from Brad on 2005-08-05 18:14

I’m having trouble implementing this plugin, but I suspect it may have more to do with my installation than the plugin. I’ve had no trouble with the standard tags etc., but I haven’t had much luck with several plugins.

I tried a version of the example code above. My sample looks like:

No comments yet


1 Comment


<$MTEntryCommentCount$> comments

But when I update my page, the final html source ends up looking like this:

No comments yet

1 Comment

0 comments


It is like the opening tags are being processed but the closing tags are not. Any suggestions? My version of perl appears to be: 5.00404

Thanks.

Comment from Tom on 2005-10-04 00:06

This MTSwitch plugin may be what I was looking for. I want the ability to insert special text or a graphic just after the 3rd entry on my home page.

I would need to somehow increment some variable in the my main template by one (i.e. x=x+1) and then use MTSwitch to check that variable to see if it’s the 3rd entry.

Do you know how would I do that using your plugin (perhaps with some other variable storage plugin)?

Comment from Matthew Anderson on 2006-01-03 22:03

I’m utilizing your plugin on my site and I get the following error only on the Comment Preview page?

Use of uninitialized value in string eq at plugins/switch.pl line 77

Comment from jyoseph on 2006-02-20 22:27

Freaking killer plugin dude. Thanks a ton.

This discussion has been closed. No further comments may be added.