if-statementconditional-statementsmovabletype

Multiple if conditions in Movable Type


I'm working on a blog project in Movable Type and I'm absolutely stuck trying to get multiple conditions in an if statement to work.

The end goal is something the equivalent of this:

<mt:SetVar name="foo" value="bar">
<mt:SetVar name="foo1" value="bar1">
<mt:If name="foo" eq="bar">
    <mt:If name="foo1" eq="bar1">
        <div>Foobar Hooray</div>
    </mt:If>
</mt:If>

I have tried as many of the usual combinations as I could think of, but is there one that I am missing? Or is this just not possible in MT?

Even the documentation doesn't reference multiple conditions. https://movabletype.org/documentation/appendices/tags/if.html

These are the attempts made (not in order of attempt) and all will show as true, even though the first condition is false:

<mt:If name="foo" eq="barf" && name="foo1" eq="bar1">
<mt:If (name="foo" eq="barf") && (name="foo1" eq="bar1")>
<mt:If name="foo" eq="barf" & name="foo1" eq="bar1">
<mt:If (name="foo" eq="barf") & (name="foo1" eq="bar1")>
<mt:If name="foo" eq="barf" AND name="foo1" eq="bar1">
<mt:If (name="foo" eq="barf") AND (name="foo1" eq="bar1")>
<mt:If name="foo" eq="barf" And name="foo1" eq="bar1">
<mt:If (name="foo" eq="barf") And (name="foo1" eq="bar1")>
<mt:If name="foo" eq="barf" + name="foo1" eq="bar1">
<mt:If (name="foo" eq="barf") + (name="foo1" eq="bar1")>

Solution

  • Your first example with nested conditionals is most commonly used in my experience, but there are a couple ways you can achieve multiple tests in a single condition block.

    First, we set the variables as you did:

    <mt:Var name="foo" value="bar">
    <mt:Var name="foo1" value="bar1">
    

    The simplest test might be in Perl using the test attribute:

    <mt:If test="$foo eq 'bar' && $foo1 eq 'bar1'">true<mt:Else>false</mt:If>
    <mt:If test="$foo eq 'bar1' && $foo1 eq 'bar'">true<mt:Else>false</mt:If>
    

    Extra caution would be prudent when using Perl in templates, and doing so will also require the server to have installed the Perl module Safe.

    That said, you could perhaps make that slightly easier to maintain if you have a large number of variables to test by relying on interpolation:

    <mt:If test='"$foo$foo1" eq "barbar1"'>true<mt:Else>false</mt:If>
    <mt:If test='"$foo$foo1" eq "bar1bar"'>true<mt:Else>false</mt:If>
    

    Or you can do something similar with normal template tags by first combining the variables (or not setting foo and foo1 in the first place, depending where their values come from):

    <mt:SetVarBlock name="meta1"><mt:Var name="foo"><mt:Var name="foo1"></mt:SetVarBlock>
    <mt:If name="meta1" eq="barbar1">true<mt:Else>false</mt:If>
    <mt:If name="meta1" eq="bar1bar">true<mt:Else>false</mt:If>
    

    The output of those tests should be:

    true
    false
    true
    false
    true
    false
    

    You should even be able to construct a SetVarBlock with each component variable on separate lines for readability with judicious use of modifiers like strip_linefeeds and trim.

    It has been quite some time since I have written much Movable Type code, so there are probably other ways. MT (and Perl) is quite flexible, and usually There Is More Than One Way To Do It. Hope that gets you closer!