Applications

RightFields 1.21

Spam Prevention

TinyTuring 1.02

Template Tags

CheckLinks 1.2

Collect 1.2

Columnize 1.11

Compare 1.1

DateTags 2.3

DaylightOrStandard 1.1

DropCap 1.1

FilterCategories 1.1

FirstNWords 1.3

GetXML 1.1

Glue 1.1

IfModified 1.4

Loop 1.1

TextWrap 1.1



Support staggernation.com's plugin development

Compare Plugin for Movable Type

Current version: 1.1 (7/12/04)

About the Compare Plugin

This Movable Type plugin implements a set of template tags for displaying a portion of a template conditionally, depending on the results of a comparison between values. The values compared can be literal strings or numbers, or they can be pieces of MT template code that the plugin will build and evaluate, using the result in the comparison.

MT users have developed a number of techniques in PHP and JavaScript to implement various useful conditional-display features on MT-based sites. The tags in the Compare plugin should let you accomplish many of these same tasks within your MT templates, without using PHP or JavaScript tricks. For example:

  • Display different text in the Comments link for each entry depending on how many comments have been posted
  • Use a different display format for entries by a particular author or set of authors
  • Within a category listing on a category archive page, display the current page's category differently from the rest
  • Display different text on an entry depending on what time of day it was entered ("morning," "afternoon," etc.)

These tags are intended for doing straight comparisons of equality and inequality. If you want to compare things against regular expressions (for instance, finding out whether a string contains another string), you should try Brad Choate's powerful and flexible Regex plugin. (The MTIfMatches tag in that plugin overlaps somewhat in functionality with the MTIfEqual and MTIfNotEqual tags in this one.)

Installation

To install the Compare plugin, upload the file Compare.pl to the plugins directory within your Movable Type directory. If you do not already have a plugins directory, create one before uploading the file. For more information about Movable Type plugins, see the documentation.

Support

Please use the support forums for all support requests, bug reports, feature requests, questions, and comments regarding this plugin.

Comparison Values

Each of the Equal/Greater/Less tags takes two required attributes, a and b. The tag compares a to b and displays its contents or not depending on the result.

The Equal tags can take additional b1, b2..., etc. attributes, as described below.

Each of the Between tags takes three required attributes, a, lower, and upper (and no b value).

Within any of these attributes, you can include one or more Movable Type template tags, by using square brackets instead of angle brackets (and no dollar signs) and single quotes instead of double quotes. (To include a literal square bracket or single quote, escape it with a backslash.) The plugin will evaluate any such tags as if they were part of a template, and use the result in the comparison.

The following example (within an MTEntries container) will display an author's nickname only if it is different from the author's name:

<MTIfNotEqual a="[MTEntryAuthor]" b="[MTEntryAuthorNickname]">
 <$MTEntryAuthorNickname$>
</MTIfNotEqual>

Comparison Options

Each of the tags can take two optional attributes:

  • numeric="1"
    By default, the values will be compared as strings (alphanumeric comparison). Pass numeric="1" to indicate that the values should be compared as numbers.

    If you compare two strings that contain numerals without using the numeric option, the results will probably be incorrect. For example, 9 is "greater than" 10 in an alphanumeric comparison.

  • case_sensitive="1"
    By default, the plugin will disregard case (capitalization) when comparing strings. Pass case_sensitive="1" to indicate that the comparison should be case-sensitive.
    <MTIfEqual a="hello" b="Hello">
     This will display.
    </MTIfEqual>
    <MTIfEqual a="hello" b="Hello" case_sensitive="1">
     This will not.
    </MTIfEqual>
    

    Obviously, using this option only makes sense if you're not also using the numeric option.

    Note that case-sensitive Greater/Less/Between string comparisons will consider lower-case letters to be greater than upper-case letters: abraham is "greater than" Zinnia.

MTIfEqual

This container tag displays its contents if and only if a is equal to b, or to any additional value named b1, b2, etc.

The following example will display a particular image for entries authored by Bob, Carol, Ted, or Alice:

<MTIfEqual a="[MTEntryAuthor]" b="Bob" b1="Carol" b2="Ted" b3="Alice">
 <img src="/images/special_people.gif">
</MTIfEqual>

MTIfNotEqual

This container tag displays its contents if and only if a is not equal to b, or to any additional value named b1, b2, etc.

The following example will display a particular template module unless the variable showed_already is equal to yes.

<MTIfNotEqual a="[MTGetVar name='showed_already']" b="yes">
 <$MTInclude module="sidebar"$>
</MTIfNotEqual>

MTIfGreater

This container tag displays its contents if and only if a is greater than b.

MTIfGreaterOrEqual

This container tag displays its contents if and only if a is greater than or equal to b.

MTIfLess

This container tag displays its contents if and only if a is less than b.

MTIfLessOrEqual

This container tag displays its contents if and only if a is less than or equal to b.

MTIfBetween

This container tag displays its contents if and only if a is between lower and upper, inclusive of those two values.

The following example displays different text depending on how many comments an entry has:

<MTIfEqual a="[MTEntryCommentCount]" b="0" numeric="1">
 No comments yet.
</MTIfEqual>
<MTIfBetween a="[MTEntryCommentCount]" lower="1" upper="10" numeric="1">
 A glimmer of interest.
</MTIfBetween>
<MTIfBetween a="[MTEntryCommentCount]" lower="11" upper="30" numeric="1">
 Quite a lively discussion!
</MTIfBetween>
<MTIfGreater a="[MTEntryCommentCount]" b="30" numeric="1">
 Shut up, already!
</MTIfGreater>

MTIfNotBetween

This container tag displays its contents if and only if a is not between lower and upper, inclusive of those two values.

MTIfBetweenExclusive

This container tag displays its contents if and only if a is between lower and upper, exclusive of those two values.

Version History

7/12/04 - version 1.1

  • Plugin now registers itself with MT 3 interface.
  • Added $VERSION variable.
  • Conditional container tags are now declared as conditional tags, so they should work with MTElse.

9/10/03 - version 1.01

  • All tags now pass conditions along when building contents, so they'll work outside conditional tags within MTEntries, etc. (ervin, Stepan Riha)

12/3/02 - version 1.0 released


The End As I Know It: A Novel of Millennial Anxiety, by staggernation.com proprietor Kevin Shay, is now available in paperback.

Please visit kshay.com for more information.