<mt:If>
A conditional block that is evaluated if the attributes/modifiers evaluate true. This tag can be used in combination with the ElseIf and Else tags to test for a variety of cases.
Attributes:
- name
- var
Declares a variable to test. When none of the comparison attributes are present ("eq", "ne", "lt", etc.) the If tag tests if the variable has a "true" value, meaning if it is assigned a non-empty, non-zero value.
- tag
Declares a MT tag to execute; the value of which is used for testing. When none of the comparison attributes are present ("eq", "ne", "lt", etc.) the If tag tests if the specified tag produces a "true" value, meaning if it produces a non-empty, non-zero value. For MT conditional tags, the If tag passes through the logical result of that conditional tag.
- op
If specified, applies the specified mathematical operator to the value being tested. 'op' may be one of the following (those that require a second value use the 'value' attribute):
- + or add
Addition.
- - or sub
Subtraction.
- ++ or inc
Adds 1 to the given value.
- -- or dec
Subtracts 1 from the given value.
- * or mul
Multiplication.
- / or div
Division.
- % or mod
Issues a modulus operator.
- + or add
- value
Used in conjunction with the 'op' attribute.
- eq
Tests whether the given value is equal to the value of the 'eq' attribute.
- ne
Tests whether the given value is not equal to the value of the 'ne' attribute.
- gt
Tests whether the given value is greater than the value of the 'gt' attribute.
- lt
Tests whether the given value is less than the value of the 'lt' attribute.
- ge
Tests whether the given value is greater than or equal to the value of the 'ge' attribute.
- le
Tests whether the given value is less than or equal to the value of the 'le' attribute.
- like
Tests whether the given value matches the regex pattern in the 'like' attribute.
- test
Uses a Perl (or PHP under Dynamic Publishing) expression. For Perl, this requires the "Safe" Perl module to be installed.
Examples:
If variable "foo" has a non-zero value:
<mt:SetVar name="foo" value="bar">
<mt:If name="foo">
<!-- do something -->
</mt:If>
If variable "foo" has a value equal to "bar":
<mt:SetVar name="foo" value="bar">
<mt:If name="foo" eq="bar">
<!-- do something -->
</mt:If>
If variable "foo" has a value that starts with "bar" or "baz":
<mt:SetVar name="foo" value="barcamp" />
<mt:If name="foo" like="^(bar|baz)">
<!-- do something -->
</mt:If>
If tag <$mt:EntryTitle$> has a value of "hello world":
<mt:If tag="EntryTitle" eq="hello world">
<!-- do something -->
</mt:If>
If tag <$mt:CategoryCount$> is greater than 10 add "(Popular)" after Category Label:
<mt:Categories>
<$mt:CategoryLabel$>
<mt:If tag="CategoryCount" gt="10">(Popular)</mt:If>
</mt:Categories>
If tag <$mt:EntryAuthor$> is "Melody" or "melody" and last name is "Nelson" or "nelson" then do something:
<mt:Authors>
<mt:If tag="EntryAuthor" like="/(M|m)elody (N|n)elson/"
<!-- do something -->
</mt:If>
</mt:Authors>
If the <$mt:CommenterEmail$> matches foo@domain.com or bar@domain.com:
<mt:If tag="CommenterEmail" like="(foo@domain.com|bar@domain.com)">
<!-- do something -->
</mt:If>
If the <$mt:CommenterUsername$> matches the username of someone on the Movable Type team:
<mt:If tag="CommenterUsername" like="(beau|byrne|brad|jim|mark|fumiaki|yuji|djchall)">
<!-- do something -->
</mt:If>
If <$mt:EntryCategory$> is "Comic" then use the Comic template module otherwise use the default:
<mt:If tag="EntryCategory" eq="Comic">
<$mt:Include module="Comic Entry Detail"$>
<mt:Else>
<$mt:Include module="Entry Detail"$>
</mt:If>
If <$mt:EntryCategory$> is "Comic", "Sports", or "News" then link to the category archive:
<mt:If tag="EntryCategory" like="(Comic|Sports|News)">
<a href="<$mt:CategoryArchiveLink$>"><$mt:CategoryLabel$></a>
<mt:Else>
<$mt:CategoryLabel$>
</mt:If>
List all categories and link to categories it the category has one or more entries:
<mt:Categories show_empty="1">
<mt:If name="__first__">
<ul>
</mt:If>
<mt:If tag="CategoryCount" gte="1">
<li><a href="<$MTCategoryArchiveLink$>"><$MTCategoryLabel$></a></li>
<mt:Else>
<li><$MTCategoryLabel$></li>
</mt:If>
<mt:If name="__last__">
</ul>
</mt:If>
</mt:Categories>
Test a variable using Perl:
<mt:If test="length($some_variable) > 10">
'<$mt:Var name="some_variable"$>' is 11 characters or longer
</mt:If>

Gautam Patel
December 14, 2007 5:20 PM | Reply
Incidentally, the converse, <mtifempty> doesn't exist. Its functionality is provided by <mt:unless>.
Anil Dash
September 1, 2008 7:43 PM | Reply
There's also a lot more information on using Movable Type's template tags as a programming language on the MT wiki.
Nate Eagle
February 27, 2009 8:49 AM | Reply
Some may find it useful to have explicit examples for using <mt:If> to evaluate two variables, or two tag values.
Comparing two variables by using $: Comparing two tag values using mt:SetVarBlock:mt:SetVarBlock can be used to store tags as values.