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

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


Related

This page was last updated on 2009-10-20, 18:23.

7 Notes

Incidentally, the converse, <mtifempty> doesn't exist. Its functionality is provided by <mt:unless>.

There's also a lot more information on using Movable Type's template tags as a programming language on the MT wiki.

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 $:

<mt:Var name="myvar1" value="foo"> <mt:Var name="myvar2" value="bar">

<mt:If name="myvar1" eq="$myvar2"> Foo is equal to bar. <mt:else> Foo is not equal to bar. </mt:If>

Comparing two tag values using mt:SetVarBlock:

mt:SetVarBlock can be used to store tags as values.

<mt:SetVarBlock name="myvar1"><$mt:TagName1$></mt:SetVarBlock> <mt:SetVarBlock name="myvar2"><$mt:TagName2$> <mt:If name="myvar1" eq="$myvar2"> TagName1 equals TagName2 <mt:Else> TagName1 does not equal TagName2 </mt:If>

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

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

If I want one result for various variable results, is this proper?

Is that right?

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)

Leave a note

Have a question? Please use the MT Forums. Notes submitted on documentation should pertain to tips & hints regarding documentation. Your note may be removed once its contents have been integrated into the body of the page.