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.
The opposite — that is, to evaluate if false — is <mt:Unless>.
Attributes
name
var
: Declares a variable to test. When none of the comparison attributes are present (“eq”, “ne”, “lt”, etc.) 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 thevalue
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
value
: Used in conjunction with theop
attribute.eq
: Tests whether the given value is equal to the value of theeq
attribute.ne
: Tests whether the given value is not equal to the value of thene
attribute.gt
: Tests whether the given value is greater than the value of thegt
attribute.lt
: Tests whether the given value is less than the value of thelt
attribute.ge
: Tests whether the given value is greater than or equal to the value of thege
attribute.le
: Tests whether the given value is less than or equal to the value of thele
attribute.like
: Tests whether the given value matches the regex pattern in thelike
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>
Compare two variables:
<mt:Var name="my_var1" value="foo">
<mt:TagName setvar="my_var2">
<mt:If name="my_var1" eq="$my_var2">
Foo is equal to bar.
<mt:else>
Foo is not equal to bar.
</mt:If>
Gautam Patel on July 29, 2009, 6:54 a.m. Reply
Note that you can use <mt:if> with a regular expression to exclude certain parameters. I believe it would go like this:
This matches everything except Webs, Sports and News.
The order is important. The
^
must come after[
, and the words to be excluded are then grouped within parentheses, separated by a|
.Andrew on September 17, 2009, 1:40 p.m. Reply
How do you refer to a Custom Index Template in an mtif statement? I’ve tried several things and it just doesn’t seem to work.
Here’s the story.
My blog does not live on the mainindex (index.html) page. It lives at newsevents (newsevents.html) which is a Custom Index Template. I want some widgets to appear on the newsevents template, but I can’t figure out how to refer to it.
Any Help?
thanks,
Andrew
ZeGuigui on January 9, 2010, 7:10 a.m. Reply
Same question than Andrew but for archive templates.
I added category based atom feeds and per-entry comments atom feed. I would like to add in the syndication widget references to those feeds (I have already added the <link rel=”alternate” > in the HTML headers)