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 the plugin in zip format.
(2004-02-26) – 0.14: This is a fix for a bug that caused 500 Internal Server Errors under certain enviroments (cgiwrap, mainly).
Copy the plugin to the Movable Type plugins directory
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
<MTSwDefault>: Container tag used to output content if none of the preceding case statements have matched.
<MTSwDefault> must be used inside of the
The ‘value’ attribute
The value attribute is available in the
<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.
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.
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.
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. 🙂