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. 🙂

15 Comments

  1. Switch

    The ‘value’ attribute will accept Movable Type tags, with <$ $>’s replaced by square brackets, and double quotes replaced by

  2. Donnie

     /  2003-05-31

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

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

  4. 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.

  5. 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.

  6. MTSwitch

    MTSwitch…

  7. Movable Type-plugin: MTSwitch

    Jeg har skrevet en liten Movable Type-plugin som gir meg grunnleggende switch-case-funksjonalitet. Det er den som gjør at du nå ser at kommentartallet er litt mer grammatisk fornuftig.

  8. Kommentar-RSS

    Ny kommentarfeed, med tilhørende mal, slik at andre kan abonnere på kommentarer gjort til innhold i bloggen

  9. New Design (3.0)

    Well ladies and gentleman, I have finally completed my redesign. Thanks to everyone for being so patient. Now, as one last piece, please allow me to explain my thinking. (For those reading on an RSS reader, please take the time and check out the sit…

  10. Petite modification pour les commentaires

    J’ai ajouté des mentions sur mesure à propos du nombre de commentaires sous les billets. Vous remarquerez que les mentions de commentaires varient selon les situations (pas de commentaire, 1 seul commentaire ou plusieurs commentaires). “Devil is in t…

  11. Including del.icio.us Inline on MT

    Alright, for all you geeks out there, you’re probably wondering how I include my del.icio.us links inline. Well, the process includes two perl scripts and three Movable Type plug…

  12. Brad

     /  2005-08-05

    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
    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.

  13. Tom

     /  2005-10-04

    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)?

  14. 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

  15. Freaking killer plugin dude. Thanks a ton.